使用cros实现跨域请求

直接上代码

后端

 this.Response.Headers.Add("Access-Control-Allow-Origin","*");

响应流里添加一个headers

这样浏览器就不会限制你的跨域请求了

当然还可以限制允许哪些域进行跨域请求

Access-Control-Allow-Origin:* 表示允许任何域名跨域访问

如果需要指定某域名才允许跨域访问,只需把Access-Control-Allow-Origin:*改为Access-Control-Allow-Origin:允许的域名

例如:header('Access-Control-Allow-Origin:http://www.client.com');

$.ajax({
type: "POST",
url: "http://127.0.0.1:8080/platform/sysUser_ToLogin",
dataType: 'jsonp',
xhrFields: {withCredentials: true},
crossDomain: true
})

这样便可以发送带cookie的请求了

如果需要设置多个域名允许访问,使用,号分割拼接就可以了

 更多查看http://www.ruanyifeng.com/blog/2016/04/cors.html

原文地址:https://www.cnblogs.com/ProDoctor/p/7486076.html