Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 440
chobo2 Avatar asked May 16 '19 17:05

chobo2


People also ask

What is the difference between 200 and 204 status code?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

Is 204 a success status code?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.

How do I fix my 204 status code?

you may indicate that a GET endpoint that returns a collection of resources will return 204 if the collection is empty. In this case GET /complaints/year/2019/month/04 may return 204 if there are no complaints filed in April 2019. This is not an error on the client side, so we return a success status code (204).

Should Put return 200 or 204?

200 OK - This is the most appropriate code for most use-cases. 204 No Content - A proper code for updates that don't return data to the client, for example when just saving a currently edited document. 202 Accepted - If the update is done asynchronous, this code can be used.


2 Answers

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.

like image 150
Elias Soares Avatar answered Nov 03 '22 23:11

Elias Soares


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.

like image 37
Tan Sang Avatar answered Nov 04 '22 00:11

Tan Sang