Phonegap返回对象Object但回显正确的数据

问题描述 投票:0回答:1

我在服务器端使用PhoneGap和PHP制作手机应用程序。 Login函数运行正常,但是AJAX返回错误[object Object],而PHP在JSON中返回正确的值。

它是为现有网站制作手机应用程序并使用其数据库。我的PHP打印的数据是正确的但我从ajax收到错误响应。警报(响应) 在出错时给出[object Object]返回值

每当我尝试提醒(response.val)我奇怪地得到未定义,但在网络中,我可以看到打印数据在JSON中是正确的。

 {{"user_id":"390","response":"Success"}}

但是当我在浏览器中查看控制台时,我发现了一个意外错误。

 Unexpected parameter ':'

我的ajax功能如下

$(document).ready(function () {
    $("#btnLogin").click(function () {
        var gebr = $("#login_username").val().trim();
        var pass = $("#login_password").val().trim();
        var dataString = "username=" + gebr + "&password=" + pass + "&login=";
        var msg = "";
        $("#message").html("Authenticating...");

        if (gebr != "" && pass != "") {
            $.ajax({
                type: "POST",
                data: dataString,
                dataType: "json",
                //contentType: "application/javascript",
                crossDomain: true,
                url: "http://url/page/app/index.php?&jsoncallback=?",
                headers: "application/json",
                success: function (response) {
                    var user_id = response.user_id;
                    var success = response.response;
                    localStorage.setItem("user_id", user_id);
                    window.location = "home.html";
                    alert("login successfull");
                },
                error: function (response) {
                    $("#message").html("error..");
                    alert(response);
                }
            });
        } else {
            msg = "Please fill all fields!";
            $("#message").html(msg);
        }
        return false;
    });

PHP

header('Content-type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST');


$gebruikersnaam = $_REQUEST['username'];
$wachtwoord = md5($_REQUEST['password']);
$user = [];


//if user and password are filled
if (!empty($gebruikersnaam) && isset($wachtwoord)) {
//checks if user_id has been found
if (!$found_user_id) { 
    $msg = "user not found!";
    print json_encode($msg);
} else {
    if (($gebruikersnaam == $user['gebruikersnaam'] && $wachtwoord == $user['wachtwoord']) && ((!empty($user['single_allowed_ip_address_for_login']) && $user['single_allowed_ip_address_for_login'] == $_SERVER['REMOTE_ADDR'])|| empty($user['single_allowed_ip_address_for_login']))) {
        //responds with this data
        $user_id = $user['user_id'];
        $response[] = array(
            'user_id' => $user_id,
            'response' => 'Success'
        );
        print json_encode($response);       
    } else {
        $msg = "user is incorrect";
        print json_encode($msg);
    }
  }
}

我一直希望获得成功的响应并将user_id保存在本地存储中,以便可以在不同的页面上使用它。

编辑:在响应中使用console.log后我得到了一个奇怪的对象。

​
     abort: function abort()​
     always: function always()​
     catch: function catch()​
     done: function add()​
     fail: function add()​
     getAllResponseHeaders: function getAllResponseHeaders()​
     getResponseHeader: function getResponseHeader()​
     overrideMimeType: function overrideMimeType()​
     pipe: function pipe()​
     progress: function add()​
     promise: function promise()
​
     readyState: 4
​
     setRequestHeader: function setRequestHeader()​
     state: function state()
​
     status: 200
​
     statusCode: function statusCode()
​
     statusText: "load"
​
     then: function then()​
     <prototype>: {…

这很奇怪,因为没有这些功能,我唯一能识别的是状态码。

php jquery ajax phonegap
1个回答
0
投票

您需要更正您的JSON。 {{"user_id":"390","response":"Success"}}不是有效的JSON。它需要是{"user_id":"390","response":"Success"}

你在console.log中得到一个奇怪的对象,因为你在response中的error: function (response)...实际上是jqXHR jQuery对象,并且不会直接在其参数中返回你的响应。坚持使用success: function(response)...并输出服务器发回的任何内容。

© www.soinside.com 2019 - 2024. All rights reserved.