ajax 跨域攜帶COOKIE


這個問題屬於Ajax跨域攜帶Cookie的問題,找了一篇博文的解決方案。

原生ajax請求方式:

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

jquery的post方法請求:

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

服務器端設置:

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


免責聲明!

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



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