为什么$ _POST从ajax请求中得不到任何东西?

时间:2012-08-20 10:33:46

标签: php ajax post

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']什么都没有,我该如何解决?

2 个答案:

答案 0 :(得分:2)

您尚未为请求正文添加内容类型。

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

答案 1 :(得分: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']