Jan Turoň
Jan Turoň

Reputation: 32912

Should I send additional headers with XmlHttpRequest POST?

After opening XmlHttpRequest POST connection

var http = new XMLHttpRequest();
http.open("POST", url, true);

Some recommend to include these additional headers before sending the request:

http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

Is it necessary on Apache+PHP server? It works fine on my server without those headers.

Upvotes: 1

Views: 266

Answers (1)

Arun Killu
Arun Killu

Reputation: 14233

your code works fine without headers because ,application/x-www-form-urlencoded is default post content-type you can see that in console.but if you are sending a file you have to exclusively set it to multipart/form-data then only server accept file.Also if in response if server doesn't send proper headers it becomes impossible to read the type of response from client eg in the case of json response if APPLICATION/JSON is needed otherwise it will be interpreted as text/html .

Upvotes: 1

Related Questions