跨域请求发送不了cookie问题: AJAX跨域请求JS配置和服务器端配置

1.ajax是同步方式

$.ajax({
type: "post",
url:url,
async:false,
data:datatosend,
dataType:"json",
beforeSend: function(xhr) {
xhr.withCredentials = true;
}
crossDomain:true,
success: function (data) {
var a=JSON.stringify(data);
if(data.result==true){
  ...........
   }else{
       ...........
     }
},
error:function (data) {
var a=JSON.stringify(data);
alert(a);
}
});

2.如果使用xhrFields:{ withCredentials:true }方法。ajax是异步方式

3.服务器端:

response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));

参考链接:http://www.cnblogs.com/helloyy/p/6109665.html
http://blog.csdn.net/wzl002/article/details/51441704

 

posted @ 2017-08-15 22:27  惊沙男孩  阅读(332)  评论(0编辑  收藏  举报