PHP $_GET 有效,但 $_POST 回显空白

标签 php ajax post get

下面是使用 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/

相关文章:

Jquery单选按钮提交值onclick

Javascript/AJAX - 从表单获取参数数组

r - 抓取页面源中不存在的数据表

php - fatal error : Cannot re-assign auto-global variable

php - 您可以从 mysqli 对象获取连接数据吗?

php - 我怎样才能防止对网站的这种自动攻击?

javascript - 使用纯 JavaScript 进行 XHR 调用

javascript - 我的 jQuery POST 请求有什么问题?

Apache Wicket 如何呈现(非 wicket)响应页面

javascript - 根据所选选项显示 SQL 查询