指引网

当前位置: 主页 > 网页制作 > JavaScript >

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

来源:网络 作者:佚名 点击: 时间:2017-08-07 22:15
[摘要] Ajax跨域请求COOKIE无法带上的解决办法
  1. 原生ajax请求方式: 
  2. var xhr = new XMLHttpRequest();   
  3. xhr.open("POST""http://xxxx.com/demo/b/index.php"true);   
  4. xhr.withCredentials = true//支持跨域发送cookies 
  5. xhr.send(); 
  6.  
  7.  
  8. jquery的ajax的post方法请求: 
  9.  $.ajax({ 
  10.                type: "POST"
  11.                url: "http://xxx.com/api/test"
  12.                dataType: 'jsonp'
  13.                xhrFields: { 
  14.                       withCredentials: true 
  15.               }, 
  16.             crossDomain: true
  17.            success:function(){ 
  18.      }, 
  19.            error:function(){ 
  20.     } 
  21. }) 
  22. 服务器端设置: 
  23. header("Access-Control-Allow-Credentials: true"); 
  24. header("Access-Control-Allow-Origin: http://www.bcty365.com"); 

 

------分隔线----------------------------