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