简体   繁体   English

AJAX $ _POST不起作用,而$ _REQUEST起作用

[英]AJAX $_POST doesn't work while $_REQUEST does

When I try to use AJAX POST, the following html doesn't work. 当我尝试使用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>

My PHP code in "ajax_test.php" is as follows: 我在“ ajax_test.php”中的PHP代码如下:

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

When I uncomment out the GET lines in my HTML, and change $_POST to $_REQUEST in my PHP, it works!! 当我取消注释HTML中的GET行,并在PHP中将$ _POST更改为$ _REQUEST时,它起作用了!

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

Why doesn't POST work? 为什么POST不起作用?

The problem is because of this line, 问题是因为这条线,

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

xhttp is not defined in your code. 您的代码中未定义xhttp

So, change 所以,改变

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

to

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM