跨域解决方案

1.加请求头

后台response添加header,response.setHeader("Access-Control-Allow-Origin", "*"); 

2.jsonp

前端主要代码(B项目---》请求A项目)

$.ajax({

         type : "GET",

         async : false,

         url : "http://localhost:8080/getInfoA?username=testB",

         dataType : "jsonp",//数据类型为jsonp 

         jsonp : "jsonpCallback",//服务端用于接收callback调用的function名的参数 

         success : function(data) {

            alert(data.result+data.username);

         },

         error : function() {

            alert('fail');

         }

      });

后台主要代码(A项目)

@RequestMapping("/getInfoA")
    @ResponseBody
    public String getInfo(HttpServletRequest request, HttpServletResponse response){
        String username = request.getParameter("username");
        String jsonpCallback = request.getParameter("jsonpCallback");
            JSONObject resultJSON = new JSONObject();
        try {
            resultJSON.put("result", "跨域请求成功");
            resultJSON.put("username", username);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return jsonpCallback + "(" + resultJSON.toString() + ")";
    }

结果显示

 

3.nginx或者springcloud 搭建网关

DEMO:https://pan.baidu.com/s/1w73vm9rhqLRFELyiuU9BSg

 

转载于:https://www.cnblogs.com/liclBlog/p/9657098.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值