Normal to see Status 204 then Status 200?
Asked Answered
B

2

9

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.

Both answered 16/5, 2019 at 17:12 Comment(4)
have you given a try to async: false, in your ajax request?. May this will resolve your issue of twice ajax requestDreda
I am using axios as my ajax library, I am not sure if it has that option.Both
You can check this for axios library which is related to async request : github.com/axios/axios/issues/681Dreda
I will check it out but I am not clear on what async: false will do. Will this basically make all calls non async?Both
S
13

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.

Sattler answered 22/5, 2019 at 2:47 Comment(3)
So my front end ajax code is hitting my api which is on it's own subdomain, I guess there is no way around this? It just kinda anoying to see these request and for some reason seem kind slow, sometimes to get this 204 back it can take anywhere from 110milliseconds to 1.97 seconds, then the 200 will take around the same time.Both
there are some sittuations that you can avoid it, but almost everytime you need it :-(Sattler
yeah, figured, do you know what might cause the lag between the request? It seems like it should be very fast.Both
P
7

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.

Product answered 22/5, 2019 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.