下面是使用 GET 正确返回字符串“Test”的 JavaScript Ajax 和 PHP:
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}
else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if (xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("content").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","selector.php?storyId=Test",true);
xmlhttp.send();
和 PHP:
<?php
$storyId=$_GET['storyId'];
echo $storyId;
?>
但是使用POST时,如下,回显空白(但确实回显)
<?php
$storyId=$_POST['storyId'];
echo $storyId;
?>
最后两行更改的 JavaScript 代码:
xmlhttp.open("POST","selector.php",true);
xmlhttp.send("storyId=Test");
为什么会发生这种情况?
最佳答案
尝试在 open()
和 send()
之间添加一个 header :
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
关于PHP $_GET 有效,但 $_POST 回显空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17758080/