简体   繁体   English

使用jQuery解析PHP Json Object。

[英]Parsing PHP Json Object using jQuery.

I am using jQuery's AJAX functionality - and I get a response back just fine, but for some odd reason I cannot parse the information inside of it! 我正在使用jQuery的AJAX功能-我得到的响应还不错,但是由于某些奇怪的原因,我无法解析其中的信息!

I am calling the following: 我打电话给以下人员:

console.log(results);   
console.log(results.data); 

And what I get is: 我得到的是:

{"data":[{"member":"asdfasdf","status":"Invalid Email"}]}
undefined 

Here is my jQuery: 这是我的jQuery:

$.ajax({
    type: "POST",
    url: "<?php echo Uri::base();?>ajax/add_members/organization",
    data: { 
        organization_id: <?php echo $organization->id;?>,
        members: $('#members').val(), 
        position: $('#position').val() 
    }
}).done(function (results) {
    // lets add them to the table
    console.log(results);   
    console.log(results.data);  
});

UPDATE: dataType: 'json', was required! 更新: dataType: 'json',是必需的!

Just because you have retrieved the string successfully in results doesn't mean it is already an object. 仅仅因为您已经成功地在results中检索了字符串,并不意味着它已经是一个对象。 You need to parse the JSON string into an object (this can be done as a shortcut depending on your actual method of calling (ie getJSON ). 您需要将JSON字符串解析为一个对象(这可以作为快捷方式完成,具体取决于您的实际调用方法(即getJSON )。

You might need to do something like this to actually get an object. 您可能需要执行类似的操作才能实际获取对象。

var obj = $.parseJSON(results);
var obj = jQuery.parseJSON('{"name":"John"}');
alert( obj.name === "John" );

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

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