- Should I ever manually set
setRequestHeader
to 'application/x-www-form-urlencoded' for ajax POST? - Should I ever manually set
setRequestHeader
to 'multipart/form-data' for ajax file upload? - Are there different requirements for XMLHttpRequest and XMLHttpRequest2 regarding
setRequestHeader
?
Common programming sense is telling me that browser can easily decide what headers should be sent depending on values from .open() and .send() methods. And that setRequestHeader
should be used only if I have some custom headers to send. But is this really the case here?
I am looking for "what's really going on under the hood" type of answer!
.
xhttp.setRequestHeader('Content-type', 'multipart/form-data'); // for files
xhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded'); // for POST escaped 'form' data
edit:
I could similarly ask for Connection or Content-length headers. Many tutorials recommend sending those headers. But in practice this is wrong. Browsers are considering those headers as unsafe
and send internally calculated values.
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
edit2:
Like in this SO question everything started to work when I removed content-type and other nonsensical headers.
Now I really want to know what is under the hood of setRequestHeader
and when should I really use it!