我在 ASP.NET Core Web API 中使用 cookie 身份驗證。當我請求 Postman 登錄時,cookie 顯示在 Postman 中。
但是當我從 ajax 請求它時,cookie 不會存盤在瀏覽器中。
這是我的 Ajax 請求 - 我在 Ajax 中遺漏了什么嗎?
$.ajax({
url: 'http://localhost:61610/api/auth/login',
method: 'POST',
xhrFields: {
'Access-Control-Allow-Credentials': true
},
data: JSON.stringify(para),
dataType: 'application/json',
contentType: 'application/json; charset=utf-8',
success: function (result, status, jqXHR) {
console.log(result);
alert(status);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
uj5u.com熱心網友回復:
你應該試試這個
xhrFields: {
withCredentials: true
}
uj5u.com熱心網友回復:
你需要在你的 ajax 中做一些改變:
$.ajax({
url: 'http://localhost:61610/api/auth/login',
method: 'POST',
xhrFields: {
withCredentials: true
},
data: JSON.stringify(a),
dataType: 'application/json',
contentType: 'application/json; charset=utf-8',
success: function (result, status, jqXHR) {
console.log(result);
alert(status);
},
error: function (jqXHR, textStatus, errorThrown) {
alert(textStatus);
}
});
然后你可以用cookie發送ajax

這里有一個更詳細解釋的鏈接:https://stackoverflow.com/questions/27406994/http-requests-withcredentials-what-is-this-and-why-using-it
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/395584.html
標籤:阿贾克斯 http asp.net-core-webapi
上一篇:從mysql到PostgreSQL,我需要在我的代碼中對php進行哪些更改?[復制]
下一篇:函式不回顯變數
