无法检测为json准备好的更改状态

时间:2015-11-13 06:26:21

标签: php json cordova

您好,我的代码是将phonegap连接到我的数据库进行登录。问题是无法检测到(xmlhttp.readyState == 4 && xmlhttp.status == 200)。这是我的代码

     var username;
 var password;

function serverURL (){
    return"http://mp13.bit-mp.biz/princesszenn";
}


 function login(){
 username = $("#username").val();
 password = $("#password").val();
 var xmlhttp = new XMLHttpRequest();
 var url = serverURL() + "/login.php";

 url += "?username=" + username + "&password=" + password;

 alert (url);
 xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
        {
        getLoginResult(xmlhttp.responseText);
        }else{
        alert("xmlerror")
        }
     }
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
 }

function getLoginResult(response) {
    var arr = JSON.parse(response);
    if (arr[0].result == "1")
    {
    localStorage.setItem("username", username);
    localStorage.setItem('password', password);
    alert("Login Success!");
    window.location.replace("index.html");
    }
    else {
    alert("Error in User Name or Password");
    $("#username").focus();
    }
}

php页面

    <?php
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");

error_reporting(E_ERROR);

    $servername = "localhost";
    $dbusername = "seetoh88_mp13";
    $password = "";
    $dbname = "seetoh88_mp13";

try{
    $conn = new mysqli($servername, $dbusername, $password, $dbname);
    $username = $_GET["username"];
    $password = $_GET["password"];

    $query = "SELECT count(*) as found from testusers where username ='" . $username . "' 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;
}
?>

0 个答案:

没有答案