【问题标题】:AJAX $_POST doesn't work while $_REQUEST doesAJAX $_POST 不起作用,而 $_REQUEST 起作用
【发布时间】:2015-12-29 23:59:25
【问题描述】:

当我尝试使用 AJAX POST 时,以下 html 不起作用。

<script>
function call_hmm_scan() {
var specie = document.getElementById("specie_name").value;
var loci = document.getElementById("locus_id").value;

var params="specie_name=" + specie + "&locus_id=" + loci

    var xmlhttp = new XMLHttpRequest();

    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("domains").innerHTML = xmlhttp.responseText;
        }
    };

//        xmlhttp.open("GET", "http://shenlab.sols.unlv.edu/kwatanabe/ajax_test.php?" + params, true);
//        xmlhttp.send();

    xmlhttp.open("POST", "http://shenlab.sols.unlv.edu/kwatanabe/ajax_test.php", true);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlhttp.send(params);
}
</script>

我在“ajax_test.php”中的PHP代码如下:

<?php
$specie = $_POST["specie_name"];
$loci = $_POST["locus_id"];
echo("$specie<br>");
echo("$loci");
?>

当我在我的 HTML 中取消注释 GET 行,并在我的 PHP 中将 $_POST 更改为 $_REQUEST 时,它可以工作!!

xmlhttp.open("GET", "http://shenlab.sols.unlv.edu/kwatanabe/ajax_test.php?" + params, true);
xmlhttp.send();

为什么 POST 不起作用?

【问题讨论】:

  • 你为什么不用jQuery.post() 来代替呢?你被项目限制了..?

标签: php ajax post


【解决方案1】:

问题是因为这条线,

xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xhttp 未在您的代码中定义。

所以,改变

xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

【讨论】:

  • 非常感谢。这解决了问题。我不敢相信我错过了!
  • @KennethWatanabe 很高兴听到。如果它解决了您的问题,请接受答案。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-25
  • 2016-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多