Ajax跨域請求COOKIE無法帶上的解決辦法
阿新 • • 發佈:2017-07-12
tar function jquery question logs request cross xmlhttp quest
本文參考https://stackoverflow.com/questions/12840410/how-to-get-a-cookie-from-an-ajax-response
原生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("https://example.com/v2/whatever", { method:‘GET‘, xhrFields: { withCredentials: true }, crossDomain: true, success: function(response){}, error: function(){} });
服務器端設置:
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Origin: http://www.xxx.com");
技術交流QQ群:15129679
Ajax跨域請求COOKIE無法帶上的解決辦法