繁体   English   中英

php 没有接收到从 xmlhttprequest 发送的数据?

[英]php not receiving data sent from xmlhttprequest?

这个网站真的只是为了娱乐。 但是当我点击按钮时,ajax 发送的发布数据没有被收到。 它不起作用。

<?php
    $recieved = $_POST["started"];
    if ($recieved == "TRUE") {
        echo 'pinging: ' . $_SERVER["REMOTE_ADDR"];
        sleep(.2);
        echo 'pinging local k3wlk1d servers';
        sleep(.2);
        echo 'executing terminal...';
        sleep(.2);
        echo 'cmd.execute:: settime 01012014';
        sleep(.2);
        echo 'cmd.execute:: ping 127.0.0.1';
        sleep(.2);
        echo 'cmd.execute:: getfiles(k3wlk1d.server, 127.0.0.1, xmlhttprequest=true)';
        sleep(.2);
        echo 'secure connection established';
        sleep(.2);
        echo 'sending to old kewlkids site...';


    }
?>
<!DOCTYPE html>
<link rel="stylesheet" href="timemachine.css">
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>K3WLK1DZ TIME MACHINE</title>
    </head>
    <body>
        <font color="red">This is an experimental time machine that will send you back to the old k3wlk1dz page. Press the button to continue.</font>
<button class="btn" onclick="ajaxstart()">Start</button>
        <p id="return"></p>
        <script>
            function ajaxstart() { /* 
                    wait 
                    WAIT
                    why am i using ajax
                    oh wait
                    i need to contact the server for... something. I forgot.
                    Darn.
                   ok
                    */
                var xmlhttp;
                if (window.XMLHttpRequest) {
                    //for ie 7, firefox, chrome, opera, safari, and *POSSIBLY* microsoft edge
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHttp");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        /* english translation: if http request is successful, run this block of code*/
                        var ret = document.getElementById("return");
                        ret.innerHTML = xmlhttp.responseText;
                    }
                    xmlhttp.open("POST", "timemachine.php", true);
                    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    xmlhttp.send("started=TRUE");
                }
            }


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

尝试在 onreadystatechange 方法之外使用 xmlhttp.open/steRequestHeader/send 方法,如下所示:

   <script>
        function ajaxstart() { /* 
                wait 
                WAIT
                why am i using ajax
                oh wait
                i need to contact the server for... something. I forgot.
                Darn.
               ok
                */
            var xmlhttp;
            if (window.XMLHttpRequest) {
                //for ie 7, firefox, chrome, opera, safari, and *POSSIBLY* microsoft edge
                xmlhttp = new XMLHttpRequest();
            } else {
                xmlhttp = new ActiveXObject("Microsoft.XMLHttp");
            }
            xmlhttp.onreadystatechange = function () {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    /* english translation: if http request is successful, run this block of code*/
                    var ret = document.getElementById("return");
                    ret.innerHTML = xmlhttp.responseText;
                }
            }

            xmlhttp.open("POST", "timemachine.php", true);
            xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
            xmlhttp.send("started=TRUE");
        }
</script>

你的 onreadystatechange 函数有问题

试试下面的代码

<?php
    $recieved = @$_POST["started"];
    if ($recieved == "TRUE") {
        echo 'pinging: ' . $_SERVER["REMOTE_ADDR"];
        sleep(.2);
        echo 'pinging local k3wlk1d servers';
        sleep(.2);
        echo 'executing terminal...';
        sleep(.2);
        echo 'cmd.execute:: settime 01012014';
        sleep(.2);
        echo 'cmd.execute:: ping 127.0.0.1';
        sleep(.2);
        echo 'cmd.execute:: getfiles(k3wlk1d.server, 127.0.0.1, xmlhttprequest=true)';
        sleep(.2);
        echo 'secure connection established';
        sleep(.2);
        echo 'sending to old kewlkids site...';


    }
?>
<!DOCTYPE html>
<link rel="stylesheet" href="timemachine.css">
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>K3WLK1DZ TIME MACHINE</title>
    </head>
    <body>
        <font color="red">This is an experimental time machine that will send you back to the old k3wlk1dz page. Press the button to continue.</font>
<button class="btn" onclick="ajaxstart()">Start</button>
        <p id="return"></p>
        <script>
            function ajaxstart() { /* 
                    wait 
                    WAIT
                    why am i using ajax
                    oh wait
                    i need to contact the server for... something. I forgot.
                    Darn.
                   ok
                    */

                var xmlhttp;
                if (window.XMLHttpRequest) {
                    //for ie 7, firefox, chrome, opera, safari, and *POSSIBLY* microsoft edge
                    xmlhttp = new XMLHttpRequest();
                } else {
                    xmlhttp = new ActiveXObject("Microsoft.XMLHttp");
                }
                xmlhttp.onreadystatechange = function () {
                    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                        /* english translation: if http request is successful, run this block of code*/
                        var ret = document.getElementById("return");
                        ret.innerHTML = xmlhttp.responseText;
                    }
                }
                xmlhttp.open("POST", "timemachine.php", true);
                xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                xmlhttp.send("started=TRUE");
            }
</script>
    </body>
</html>

希望这会有所帮助

问候

暂无
暂无

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

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