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>  

 

posted on 2017-09-17 03:02  --LP--  阅读(294)  评论(0编辑  收藏  举报

导航