登录 跨域 带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
最新推荐文章于 2022-12-22 11:08:00 发布