1
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = action;
httpRequest.open('POST','/fetch_product_list.php','true');
httpRequest.send("var=5");

但在fetch_product_list.php, $_POST['var'] 什么都没有,我该如何解决?

4

2 回答 2

2

您没有为请求正文包含内容类型。

httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
于 2012-08-20T10:38:24.277 回答
2

添加行:httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");因此代码如下所示:

var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = action;
httpRequest.open('POST','/fetch_product_list.php','true');
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
httpRequest.send("var=5");

并在 php 中使用:$_POST['var'].

于 2012-08-20T10:51:27.153 回答