AJAX:为什么服务器端的$_REQUEST不包含GET方法在URL中从JS传递的变量


AJAX: Why does $_REQUEST on server side not contain the variable passed from JS in URL by GET method?

我有三个div作为simple_html_dom对象保存在array中。我需要在点击一个按钮时更改其中两个的CSS属性。这很容易,但我还需要在服务器端的PHP脚本中的CSS属性(存储在前面提到的数组中的simple_html_dom对象中)中进行更改

因此,我从我的网络搜索中发现,我需要AJAX来实现这一点。因此,我阅读了本教程,并按照这个例子进行了如下操作:

在客户端:

function xyz(var divIdOne, var divIdTwo) {
    document.getElementById(params.divIdOne).style.display = "none";
    document.getElementById(params.divIdTwo).style.display = "block";
    document.getElementById(params.divIdTwo).style.border = "5px solid red";
    var xmlhttp;
    if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest();}
    else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    xmlhttp.open("GET","myfile.php?pass_back="+"pass_back",true);
    xmlhttp.send();
}

在服务器端:

foreach($_REQUEST as $requestkey => $requestvalue) {
    echo $requestkey.': '.$requestvalue;
}
if (array_key_exists('pass_back', $_REQUEST)) {
    foreach ($array_of_divs as $div) {
        if ($div->id=$divIdOne) {
            $div->style='display:none';
        } else if ($div->id=$divIdTwo) {
            $div->style='display:block';
        }
    }
} else {echo 'FALSE!';}

第一个foreach loop打印其他变量,但不打印pass_back。下一个if块根本不执行。执行else块。这意味着$_REQUEST显然不包含pass_back。有人能指出为什么,或者我做错了什么吗?

我没有看到错误。你如何检查你没有正确发送数据?我建议使用这段代码来检查您的服务器从您的请求中接收到的内容:

function xyz() {
        var xmlhttp;
        if (window.XMLHttpRequest) {
            xmlhttp = new XMLHttpRequest();
        } else {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                console.log(xmlhttp.responseText);// Let's see what the server is echoing
            }
        }
        xmlhttp.open("GET","myfile.php?pass_back="+"pass_back",true);
        xmlhttp.send();
    }

请告诉我们产量。

我建议您使用jquery和firebug,首先解决以下问题:为什么没有接收到passback变量。

创建文件:test.php

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <script src="https://code.jquery.com/jquery-2.1.1.js"></script>
    <title>Html page</title>
</head>
<body>

<script>
    (function($){
        $.post("myfile.php", {
            passback: "pooo"
        }, function(d){
            console.log(d);
        });

    })(jQuery);
// or if you cannot use jquery
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
        // do something with the response, if you need it...
        // document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
    }
}
xmlhttp.open("POST", "myfile.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send("passback=pooo");
// src:  http://www.w3schools.com/ajax/ajax_xmlhttprequest_create.asp

</script>
</body>
</html>

然后创建文件:myfile.php

<?php

if(isset($_REQUEST['passback'])){
    echo "ok";
}
else{
    echo "oops";
}

接下来,下载firefox,并下载firebug扩展(或者您也可以使用chrome的控制台)。这里的目标是查看通过网络传递的内容。

在浏览器中启动test.php,打开firebug控制台(或chrome的控制台),并进行调试,直到您看到"ok"消息。