【问题标题】:xmlhttp.status returns 0 in localhost XAMPPxmlhttp.status 在 localhost XAMPP 中返回 0
【发布时间】:2016-11-14 07:48:54
【问题描述】:

我是一名初学者,正在为我的学校作业开发应用程序。 目前,我正在使用 XAMPP 为我的数据库托管我的应用程序和 phpmyadmin。 我面临的问题是 xmlhttp.status 返回 0 而不是 200。我以前使用 microsoft azure 而不是 XAMPP 的项目没有这个问题。

这是我在 index.html 中的脚本代码

var userid;
        var password;
        function serverURL(){
         return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
            }
        function login(){
        userid = $("#userid").val();
        password = $("#password").val();
        if (validate()){
            alert("validate pass");
        var xmlhttp = new XMLHttpRequest();
        var url = serverURL() + "/login.php";
        url += "?userid=" + userid + "&password=" + password;
        alert(url);
        xmlhttp.onreadystatechange=function() {
            alert("xmlhttponready running");
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            alert(xmlhttp.status);
            alert(xmlhttp.readyState);

            }
        getLoginResult(xmlhttp.responseText);
        }


        xmlhttp.open("GET", url, true);
        xmlhttp.send();
        }
        }

这是我的 login.php

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ERROR);
try{
    $conn = new mysqli("127.0.0.1", "root", "root", "bencoolen");
    $userid = $_GET["userid"];
    $password = $_GET['password'];
    $query = "SELECT count(*) as found from profiles where userid ='" .
            $userid . "' and password = '" . $password . "'";
    $result = $conn->query($query);
    $count = $result->fetch_array(MYSQLI_NUM);
    $json_out = "[" . json_encode(array("result"=>$count[0])) . "]";
    echo $json_out;
    $conn->close();
}
catch(Exception $e) {
    $json_out = "[".json_encode(array("result"=>0))."]";
    echo $json_out;

}

PHP 返回 result='0' 我个人觉得问题可能出在

function serverURL(){
             return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
                }

请指教:(

【问题讨论】:

    标签: javascript php ajax azure xampp


    【解决方案1】:

    一些小错误:

    您的 saveUrl 函数返回“http://localhost/webP/FYP/workshop/platforms/android/assets/www/

    现在当您尝试连接时

    var url = serverURL() + "/login.php";
    

    网址变成 "http://localhost/webP/FYP/workshop/platforms/android/assets/www//login.php"

    我希望你想要 json 响应。那你为什么要加? "[".json_encode()."]"

    json_encode(array("result"=>0))
    

    输出:

    {"result":0}
    

    getLoginResult(xmlhttp.responseText);
    

    您正在远离 if 块。这是您得到 0 响应的主要原因。为此,让我稍微解释一下 xmlhttp

    当一个ajax请求被触发时,它以4个状态完成

    state value = 0 :=> (state = UNSET): the xmlhttp instance is initiated 
    state value = 1 :=> (state = OPENED): The browser sends the data to the server
    state value = 2 :=> (state  = HEADERS_RECEIVED): request reached at server
    state value = 3 :=> (state  = LOADING): server processing the request
    state value = 4 :=> (state  = DONE): response reached from server to browser
    

    现在进入编码部分:

        // When ever there is a state chgange in
        // the xmlhttp object
        xmlhttp.onreadystatechange=function() {
            alert(xmlhttp.readyState);
    
            // when the response from server is reached (state = 4)
            // and the response header http code is 200 (xmlhttp.status == 200)
            // do the following
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                alert(xmlhttp.status);
                alert(xmlhttp.readyState);
                getLoginResult(xmlhttp.responseText);
            }
    
        }
    

    但是正如您所保留的那样 getLoginResult(xmlhttp.responseText);超出 if 块,所以它在状态为零时执行(意味着当请求没有发送到服务器时)

    试试这个js代码:

    var userid;
    var password;
    function serverURL() { 
        return "http://localhost/webP/FYP/workshop/platforms/android/assets/www/";
    }
    
    function login(){
        userid = $("#userid").val();
        password = $("#password").val();
        if (validate()){
            alert("validate pass");
            var xmlhttp = new XMLHttpRequest();
            var url = serverURL() + "login.php";
            url += "?userid=" + userid + "&password=" + password;
            alert(url);
            xmlhttp.onreadystatechange=function() {
                alert("xmlhttponready running");
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    alert(xmlhttp.status);
                    alert(xmlhttp.readyState);
                    getLoginResult(xmlhttp.responseText);
                }
    
            }
            xmlhttp.open("GET", url, true);
            xmlhttp.send();
        }
    }
    

    还有你的 php 代码:

    <?php
    header("Access-Control-Allow-Origin: *");
    header("Content-Type: application/json; charset=UTF-8");
    error_reporting(E_ERROR);
    try{
        $conn = new mysqli("127.0.0.1", "root", "root", "bencoolen");
        $userid = $_GET["userid"];
        $password = $_GET['password'];
        $query = "SELECT count(*) as found from profiles where userid ='" .
                $userid . "' and password = '" . $password . "'";
        $result = $conn->query($query);
        $count = $result->fetch_array(MYSQLI_NUM);
        $json_out = json_encode(array("result"=>$count[0]));
        echo $json_out;
        $conn->close();
    }
    catch(Exception $e) {
        $json_out = json_encode(array("result"=>0));
        echo $json_out;
    
    }
    ?>
    

    建议

    当您使用 jquery 时,请尝试使用 jquery.ajax() 代替 XMLHttpRequest(如果您是为了学习目的而这样做,那么很好)。这将解决您的浏览器兼容性问题。

    为避免 sql 注入,请尝试使用 mysqli::prepare。 http://php.net/manual/en/mysqli.prepare.php

    有关 XMLHTTPRequest 的更多信息:https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/readyState

    【讨论】:

    • 感谢您的深入回复!很快就会给您回复结果!
    猜你喜欢
    • 1970-01-01
    • 2013-06-12
    • 1970-01-01
    • 2016-05-05
    • 2015-06-15
    • 1970-01-01
    • 2016-01-21
    • 2015-08-21
    • 1970-01-01
    相关资源
    最近更新 更多