When using the following:
xhr.setRequestHeader("Content-Type", "application/json");
Am I obligated to only receive a response in json
format, or is it possible to receive an html
response instead?
If there is flexibility in receiving different formats, how can I dynamically handle these two aspects:
- The method of parsing the response (based on whether it's json or not)
- Which
Accept
header to utilize
For instance, should my function include:
xhr.setRequestHeader('Accept', "application/json");
when expecting a json response and also performing a JSON parse on the response, but omitting this for an html response.
Therefore, is there a way to dynamically manage the response handling?