是否应该使用XmlHttpRequest POST发送其他标头? [英] Should I send additional headers with XmlHttpRequest POST?

查看:91
本文介绍了是否应该使用XmlHttpRequest POST发送其他标头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

打开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");

在Apache + PHP服务器上是否有必要?如果没有这些标头,它可以在我的服务器上正常工作.

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

推荐答案

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

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 .

这篇关于是否应该使用XmlHttpRequest POST发送其他标头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆