duanhu7390 2019-02-19 10:53
浏览 71

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

I'm making a phone app using PhoneGap with PHP on the server side. the Login function has been working fine, but AJAX returns an error [object Object] while PHP returns the correct value's in JSON.

it's for making a phone app for an existing website and using its database. the data my PHP prints is correct yet I receive an error response from ajax. Alert(response)
Gives an [object Object] return value on error

whenever I try alert(response.val) I strangely get undefined, but in the network, I can see the printed data is correct in JSON.

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

but when I look in the console on my browser I see an unexpected error.

 Unexpected parameter ':'

my ajax function is as follows

$(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);
    }
  }
}

I have been wanting to get a successful response and saving the user_id in local storage so it can be used on a different page.

Edit: after using console.log on the response i got an odd object.

​
     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>: {…

which is strange because none of these functions are made and the only thing which i can recognize is the statuscode.

  • 写回答

1条回答 默认 最新

  • dongtuo7364 2019-03-28 21:46
    关注

    You need to correct your JSON. {{"user_id":"390","response":"Success"}} is not valid JSON. It needs to be {"user_id":"390","response":"Success"}.

    You're getting a strange object in your console.log because your response in the error: function (response)... is actually the jqXHR jQuery object and will not return your response directly in its arguments. Stick with success: function(response)... and output whatever your server sends back.

    评论

报告相同问题?

悬赏问题

  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图