我应该使用XmlHttpRequest POST发送额外的标头吗?

时间:2013-03-28 07:18:01

标签: javascript ajax apache post xmlhttprequest

打开XmlHttpRequest POST连接后

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

Some建议在发送请求之前包含这些额外的标头:

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

Apache + PHP服务器上是否有必要?它在没有这些标题的服务器上正常工作。

1 个答案:

答案 0 :(得分:1)

您的代码在没有标题的情况下工作正常,因为application/x-www-form-urlencoded是默认post content-type您可以在控制台中看到它。但如果您是sending a file,则必须将其专门设置为multipart/form-data然后只有服务器接受file.Also如果在响应中如果服务器没有发送正确的头,则无法从客户端读取响应的类型,例如在json响应的情况下,如果需要APPLICATION/JSON,否则它将被解释为text/html