Ajax跨域請求COOKIE無法帶上的解決辦法


博客搬家了,歡迎大家關注,https://bobjin.com
1、原生ajax請求方式:
1 var xhr = new XMLHttpRequest();  
2 xhr.open("POST", "http://xxxx.com/demo/b/index.php", true);  
3 xhr.withCredentials = true; //支持跨域發送cookies
4 xhr.send();
2、jquery的ajax的post方法請求: 
 $.ajax({
        type: "POST",
        url: "http://xxx.com/api/test",
        dataType: 'json',
    // 允許攜帶證書
        xhrFields: {
             withCredentials: true
        },
    // 允許跨域
        crossDomain: true,
        success:function(){
        },
        error:function(){
    }
})
3、服務器端設置: 
 
        
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");

轉自http://blog.sina.com.cn/s/blog_87b9bbc70102vg18.html

博客搬家了,歡迎大家關注,https://bobjin.com


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM