美文网首页
Ajax跨域请求COOKIE无法带上的解决办法

Ajax跨域请求COOKIE无法带上的解决办法

作者: pgf | 来源:发表于2017-12-18 11:59 被阅读0次

原生ajax请求方式:

var xhr = new XMLHttpRequest();
xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);
xhr.withCredentials = true; //支持跨域发送cookies
xhr.send();

jquery的ajax的post方法请求:

$.ajax({

           type: "POST",
           url: "http://xxx.com/api/test",
           dataType: 'jsonp',
           xhrFields: {
                  withCredentials: true
          },
        crossDomain: true,

       success:function(){

 },

       error:function(){

}

})

服务器端设置:

header("Access-Control-Allow-Credentials: true");

header("Access-Control-Allow-Origin: http://www.xxx.com");

相关文章

网友评论

      本文标题:Ajax跨域请求COOKIE无法带上的解决办法

      本文链接:https://www.haomeiwen.com/subject/hwopwxtx.html