简体   繁体   中英

Normal to see Status 204 then Status 200?

I am looking at my ajax request in network tab in chrome and I noticed every ajax request I do happens twice.

First one is a 204 and then followed up with 200. My ajax call is only being hit once so I am not sure why there are 2.

Edit

So it seems to have to do with Cors, which I have just set to star (*) for testing.

I guess there is not to much I can do to not have it do 2 requests, but what really gets me is why it takes so long, I looking at google chrome network and on my page these 204 took anywhere from 110ms to 1.97 seconds.

When you try to send a AJAX request to a different domain, you are violating the same-origin policy.

Server that you are sending the request allows cross domain requests. In the process, there should be a preflight call and that is the HTTP OPTION call.

So, you are having two responses for the OPTION and your result.

That's a consequence of the CORS - Cross-origin resource sharing "protocol".

When doing requests do other domains, the browser do a request before your request, asking for the server if it can proceed with that request.

This request uses the method OPTIONS and should have no content, just response headers, that's why the response code is 204 (no content). After confirming that the request is allowed, the browser proceed with your request, that now will return 200 (or any other) status code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM