xmlhttp.status在localhost XAMPP中返回0

时间:2016-07-12 07:33:17

标签: javascript php ajax azure xampp

我是初学者,正在为我的学校工作做一个应用程序。 目前,我正在使用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/";
                }

请告知:(

1 个答案:

答案 0 :(得分: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时,尝试使用XMLHttpRequest的jquery.ajax()instaed(如果你是为了学习目的而做,那就好了)。这将解决您的浏览器兼容性问题。

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

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