登錄 跨域 帶cookie解決方案 ajax done fail方法.txt
參考:https://www.cnblogs.com/eret9616/p/9661314.html
摘自此文的末尾。
4.2 解決請求帶上Cookie信息
完整代碼如下:
$.ajax({
url: "http://localhost:8020/api/login.php",
type: "POST",
data: {
username: $("#username").val(),
password: $("#password").val()
},
dataType: "json",
xhrFields: { // 此處設置 get cookie。
withCredentials: true
}
}).done(function (response) {
debugger;
$("#log").html(response.message);
window.location.href = "home.html";
}).fail(function (jqXHR, textStatus) {
console.log("Request failed: " + textStatus);
});
後端php代碼
/*需要設置這一行,接收傳入Credentials字段,即,cookie。*/
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Allow-Origin: http://localhost:8010');
登錄 跨域 帶cookie解決方案 ajax done fail方法.txt
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.