I am using cookie authentication in ASP.NET Core Web API. When I am requesting for login from Postman, cookie is shown in Postman.
But when I am requesting it from ajax, the cookie does not get stored in the browser.
Here is my Ajax request - am I missing anything in 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);
}
});
CodePudding user response:
you should try this
xhrFields: {
withCredentials: true
}
CodePudding user response:
You need do some change in your 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);
}
});
then you can send ajax with cookie
Here is a link with a more detailed explanation :https://stackoverflow.com/questions/27406994/http-requests-withcredentials-what-is-this-and-why-using-it