How does a server detect a request made with the fetch API? How does a server detect a request made with the fetch API? json json

How does a server detect a request made with the fetch API?


Either of a specific header or a specific parameter would work; the "clean" approach would probably be to set the "Accept:" header in the fetch-sent request to "application/json", signalling that the said request is looking for a JSON response, and use that on the server-side to determine whether to send JSON or HTML.


I think Accept Header does not help for the all scenarios. If you request HTML and you have a layout system and want to disable it for async request you will need a different approach.

Using a custom flag header is a more guarantied approach I think.