简体   繁体   English

从ajax调用中检索json_encoded PHP对象值?

[英]Retrieving json_encoded PHP object values from an ajax call?

My ajax: 我的ajax:

$.ajax(
        {
            type:'POST',
            url: 'ajax.php',                  //the script to call to get data
            data: {request: 'getUser',id:id},
            dataType: 'json',                //data format
            complete: function(data)          //on receive of reply
            {
                console.log(data); 
            }
        });

My php file that handles the ajax request(ajax.php): 我的处理ajax请求的php文件(ajax.php):

elseif ($_POST['request'] == 'getUser')
    {
        $DAO = new UserDAO;
        $q = $DAO->ajaxGetUser($_POST['id']);
        echo json_encode($q);
    }

ajaxGetUser function: ajaxGetUser函数:

public function ajaxGetUser($id)
    {
        $q = $this->db->prepare('SELECT * FROM user WHERE userId=:id');
        $q->bindValue(':id', $id, PDO::PARAM_INT);
        $q->execute();
        $r = $q->fetch(PDO::FETCH_OBJ);
        unset($r->userPassword);
        return $r;
    }

console.log(data) is showing me the object in the "ResponseJSON" on firebug, but when I try something like console.log(data.userName) , console.log(data[0].userName) , they're undefined, since i'm not very good in ajax i've been looking many threads but couldn't find one that could help me. console.log(data)向我显示萤火虫上“ ResponseJSON”中的对象,但是当我尝试类似console.log(data.userName)console.log(data[0].userName) ,它们是未定义的,由于我对ajax的掌握不是很好,所以我一直在寻找许多线程,但是找不到可以帮助我的线程。

I guess the json is already parsed since dataType is set to "json", how can I access the User object with all its properties? 我猜因为dataType设置为“ json”,所以json已被解析,我如何访问User对象的所有属性? Thanks for your help 谢谢你的帮助

readyState 4

responseJSON
    Object { userId="6", userName="321", more...}

responseText
    "{"userId":"6","userName...":null,"userStatus":"0"}"

status 200  
statusText "OK" 
abort function()    
always function()   
complete function()
done function()
error function()
fail function()
getAllResponseHeaders function()    
getResponseHeader function()
overrideMimeType function()
pipe function()
progress function()
promise function()
setRequestHeader function()
state function()
statusCode function()
success function()  
then function()

Set a success handler instead of complete : 设置success处理程序,而不是complete

The callback hooks provided by $.ajax() are as follows: $ .ajax()提供的回调挂钩如下:

[...] [...]
4. success callback option is invoked, if the request succeeds. 4.如果请求成功,则调用成功回调选项。 It receives the returned data, a string containing the success code, and the jqXHR object. 它接收返回的数据,包含成功代码的字符串以及jqXHR对象。
[...] [...]
6. complete callback option fires, when the request finishes, whether in failure or success. 6.完成请求时,无论失败还是成功,都会触发complete回调选项。 It receives the jqXHR object, as well as a string containing the success or error code. 它接收jqXHR对象,以及包含成功或错误代码的字符串。

The code would look like: 代码如下所示:

success: function(data)
{
    console.log(data, data.userId, data.userName); 
}

It seems data was an object, I managed to get the user object was by using data.responseJSON , so data.responseJSON.userName for the name. 这似乎是数据对象,我设法让用户对象是通过使用data.responseJSON ,所以data.responseJSON.userName的名称。

I don't know why it would return in this way through. 我不知道为什么它会以这种方式返回。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM