PHP没有获得xmlhttprequest帖子

时间:2016-01-03 04:40:22

标签: php jquery

我已经在这里工作了几个小时,还没有找到解决方案。 Chrome的网络活动显示正在发送帖子,有效负载为" id = a1"但是php 总是返回一个空字符串。要么这是我最长时间错过的一些愚蠢的错字,要么我的服务器主机php.ini正在阻止它。我考虑过后者,但我之前在这台主机上使用了大量的帖子脚本而没有问题。

我已经尝试过我能想到的每一个组合都无济于事:(

导致这种情况的原因是什么?

相关.js:



var xhr;
if (window.XMLHttpRequest) {
  xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
  xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

xhr.open("POST", "api.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www/form-urlencoded");
xhr.send("id=" + "a1");
xhr.onreadystatechange = display_data;

function display_data() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    alert(xhr.responseText);
  }

}




相关.php:



$id = $_POST['id'];
echo $id;




1 个答案:

答案 0 :(得分:1)

试试这个......

编辑。错过了分号。



var xhr;
if (window.XMLHttpRequest) {
  xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
  xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

xhr.open("POST", "api.php", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("id=" + "a1");
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    alert(xhr.responseText);
  }

};