Parsing PHP Json Object using jQuery.

I use jQuery AJAX functionality - and I get the answer back just fine, but for some odd reason I can't parse the information inside it!

I call the following:

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

And I get:

 {"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', required!

+4
source share
2 answers

Just because you successfully returned the string to results does not mean that 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 calling method (i.e. getJSON ).

You may need to do something similar to get the object.

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

All Articles