LukePOLO
LukePOLO

Reputation: 1118

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!

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:

$.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!

Upvotes: 4

Views: 1207

Answers (2)

Mike Brant
Mike Brant

Reputation: 71384

Just because you have retrieved the string successfully in results doesn't mean it is already an object. You need to parse the JSON string into an object (this can be done as a shortcut depending on your actual method of calling (i.e getJSON).

You might need to do something like this to actually get an object.

var obj = $.parseJSON(results);

Upvotes: 2

jsteinmann
jsteinmann

Reputation: 4752

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

Upvotes: 0

Related Questions