Hello, I have a problem with an AJAX post request in jquery, since it generates two requests, one POST and the other OPTIONS, I don't know if this will be a problem in the long run or if the API gets confused.
Why does that OPTIONS request always appear? Could it be removed?
$.post("http://127.0.0.1:8000/newClient/", {
data: JSON.stringify(validData),
}, function (response) {
var r = JSON.parse(response);
if (r.status != 200) {
$('#err').html(r.content).css('color', 'red');
}
else {
$('#err').remove();
}
})
The API is in Django and these are the response headers it generates for me.
POST header
OPTIONS header
This is called a request
preflighted
and it is not a problem, it is a request that the browser makes to your server to verify that the request you are trying to make is safe.Why?
The reason this request is sent is because you are trying to make a request
POST
to another domain (CORS
) in your case127.0.0.1:8000
.Therefore chrome makes a request
preflighted
to check that ithttp://127.0.0.1:8000
allows the request to this domain.Summary
Since this request is made by the browser for security reasons, it is more than justified.
And it does not alter the original request, the data you receive in the response will be what you expect.