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'] 什么都没有,我该如何解决?
您没有为请求正文包含内容类型。
httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
添加行: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']
.