Should I send additional headers with XmlHttpRequest POST?

夙愿已清 提交于 2019-12-23 02:53:05

问题


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.


回答1:


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 .



来源:https://stackoverflow.com/questions/15676263/should-i-send-additional-headers-with-xmlhttprequest-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!