简体   繁体   English

XMLHttpRequest,设置了setRequestHeader时,$ _ POST为空

[英]XMLHttpRequest, $_POST is null when setRequestHeader is set

I wrote javascript code with PHP, and wanted to send request using XMLHttpRequest. 我用PHP编写了JavaScript代码,并想使用XMLHttpRequest发送请求。 But always get $_POST null, when setRequestHeader is set. 但是,在设置setRequestHeader时,始终使$ _POST为null。 If I delete setRequestHeader, the $_POST is exist. 如果删除setRequestHeader,则$ _POST存在。

Here's my javascript code 这是我的JavaScript代码

var xmlhttp = new XMLHttpRequest();
var params = new FormData();
params.append('workername', name);
params.append('address', address);
params.append('email', email);
xmlhttp.open("POST", "searchworker.php", true);
//Send the proper header information along with the request
//xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        document.getElementById("workerResult").innerHTML = this.responseText;
    } 
};


xmlhttp.send(params);

The code result for 的代码结果

print_r($_POST);`

is

Array ( [workername] => green [address] => US [email] => test@example.com ) 数组([workername] =>绿色[address] => US [email] => test@example.com)

I can receive $_POST value. 我可以收到$ _POST值。 But if uncomment the line 但是如果取消注释线

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

I will get this result 我会得到这个结果

Array ( [------WebKitFormBoundaryTBeUMaDsrWBBYRFB Content-Disposition:_form-data;_name] => "workername" Array([------ WebKitFormBoundaryTBeUMaDsrWBBYRFB Content-Disposition:_form-data; _name] =>“ workername”

green ------WebKitFormBoundaryTBeUMaDsrWBBYRFB Content-Disposition: form-data; 绿色------ WebKitFormBoundaryTBeUMaDsrWBBYRFB内容处置:表格数据; name="address" NAME = “地址”

US ------WebKitFormBoundaryTBeUMaDsrWBBYRFB Content-Disposition: form-data; 美国------ WebKitFormBoundaryTBeUMaDsrWBBYRFB内容处置:表格数据; name="email" NAME = “电子邮件”

test@example.com ------WebKitFormBoundaryTBeUMaDsrWBBYRFB-- test@example.com ------ WebKitFormBoundaryTBeUMaDsrWBBYRFB--

)

And all $_POST value is null. 并且所有$ _POST值都为null。

So should I use the setRequestHeader? 那么我应该使用setRequestHeader吗? Cause I read on the web, that if we use open("POST",...) we should use setRequestHeader. 因为我在网上读到,如果我们使用open(“ POST”,...),我们应该使用setRequestHeader。 What's causing $_POST cannot be received if setRequestHeader is set? 如果设置了setRequestHeader,是什么原因导致无法接收到$ _POST?

A FormData object, when posted, is formatted into a multipart/form-data body, not a urlencoded one. FormData对象在发布时会被格式化为multipart/form-data主体,而不是经过urlencoded的主体。 If you want to post the data as application/x-www-form-urlencoded , you'll need to format the post body into an urlencoded string yourself. 如果要将数据发布为application/x-www-form-urlencoded ,则需要自己将发布正文格式化为urlencoded字符串。

var params = 'workername=' + encodeURIComponent(name) + '&address=' + encodeURIComponent(address) + '&email=' + encodeURIComponent(email);

Leave out the Content-Type header. 省略Content-Type标头。 The browser knows how to set up a FormData request. 浏览器知道如何设置FormData请求。

If you look in browser dev tools network and inspect the actual headers that are sent you will see it is set as something like: 如果查看浏览器开发工具网络并检查发送的实际标头,则会看到它设置为:

Content-Type:multipart/form-data; boundary=-----11411516529451

Also note you can pass a form element into FormData and simplify having to do all the manual appends yourself 还要注意,您可以将表单元素传递到FormData并简化必须自己完成所有手动附加操作的过程

var params = new FormData(document.forms[0]);// or other appropriate dom query

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM