zoukankan      html  css  js  c++  java
  • ajax请求跨域

    解决方式 1:

    解决方式 2:

    服务端:

    package ceshi_utils;
    
    import java.util.*;
    
    import com.xwhb.utils.encrypt.CipherUtil;
    import com.xwhb.utils.encrypt.MD5Util;
    
    import io.vertx.core.AbstractVerticle;
    import io.vertx.core.eventbus.DeliveryOptions;
    import io.vertx.core.http.HttpMethod;
    import io.vertx.core.http.HttpServer;
    
    public class CSHttpVerticle extends AbstractVerticle {
    
    
        @Override
        public void start() throws Exception {
            super.start();
    
            HttpServer server = vertx.createHttpServer();
            server.requestHandler(req -> {
                if (req.method() == HttpMethod.GET) {
    
                    if (req.path().equals("/xwh-order")) {
                        req.response().setChunked(true);
                        req.bodyHandler(buffer -> {
    
                            String way = req.getParam("way");
    
                            System.out.println("way :"+way);
    
                            if (null != way){
                                way = "WX";
                            }else {
                                way = "ZFB";
                            }
    
                            String sr=HttpRequest.sendPost("http://47.96.12.223/xwhbank", generateParams_order(way));
                              System.out.println(sr);
    
                            req.response().putHeader("Access-Control-Allow-Origin","*");//allow all ip
                            req.response().putHeader("Access-Control-Allow-Methods","Get,Post,Put,OPTIONS");
                            req.response().putHeader("Access-Control-Allow-Headers","X-Requested-With,Content-Type,Accept");
    
                              req.response().putHeader("Content-Type", "text/html;charset=utf-8");
                              req.response().setStatusCode(200).write(sr).end();
                        });
                        
                        
                    }else if(req.path().equals("/xwh-withdraw")){
                        req.response().setChunked(true);
                        req.bodyHandler(buffer -> {
    
                            String sr=HttpRequest.sendPost("http://47.96.12.223/xwhbank", generateParams_withdraw());
                            System.out.println(sr);
    
                            req.response().putHeader("Access-Control-Allow-Origin","*");//allow all ip
                            req.response().putHeader("Access-Control-Allow-Methods","Get,Post,Put,OPTIONS");
                            req.response().putHeader("Access-Control-Allow-Headers","X-Requested-With,Content-Type,Accept");
    
                            req.response().putHeader("Content-Type", "text/html;charset=utf-8");
                            req.response().setStatusCode(200).write(sr).end();
    
                        });
                    }else {
                        req.response().setStatusCode(500).end();
                    }
                }
            });
            server.listen(8123);
        }
    
        private String generateParams_withdraw() {
        ...
        }
    
        private String generateParams_order(String way){
           ...
        }
        
        
    }

    浏览器端

    <!DOCTYPE html>
    <html>  
    <head>  
    
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>  
        <meta http-equiv="Access-Control-Allow-Origin" content="*">    这行测试时不要也可以
        <title>登录</title>  
        <script type='text/javascript' src='http://cdn.staticfile.org/jquery/2.1.1/jquery.min.js'></script>
        <script type="text/javascript" src="http://cdn.staticfile.org/jquery.qrcode/1.0/jquery.qrcode.min.js"></script> 
        
    </head>  
    <body>  
    <div id="qrcode"></div>     
        <script type="text/javascript">
            $.ajax({
                type: "GET",
                url: "http://47.96.146.122:8123/xwh-order?way=WX",
                dataType: "text",
                success: function (data) {                            
                    $('#qrcode').qrcode(data);
                }
            });
        </script>
    </body>  
    </html>  
  • 相关阅读:
    Android零基础入门第34节:Android中基于监听的事件处理
    【洛谷】3953:逛公园【反向最短路】【记忆化搜索(DP)统计方案】
    【洛谷】1608:路径统计 1144:最短路计数
    【洛谷】1081:跑路【倍增】【最短路】
    照着例子学习protobuf-python
    NodeJs与ActionScript的GET和POST通讯
    编程语言应用领域(转)
    我是C#上层转到嵌入式和单片机的
    转发一位老师的文章,希望能给你带来帮助
    沈逸的IT专栏博客记录
  • 原文地址:https://www.cnblogs.com/LiuPan2016/p/7534517.html
Copyright © 2011-2022 走看看