$post / $each with JQuery

Let's say you want to do a JQuery $post and then use $each to traverse through the result. this exampel show how it can be accomplished.



Example JSON data set generated using Php or other scripting languages :-

{"items":[{"memberID":"1","memberName":"Julia Boris","profileInfo":"jaya jaya"},{"memberID":"1","memberName":"Julia Boris","profileInfo":"michael "},{"memberID":"1","memberName":"Julia Boris","profileInfo":"89898989"}]}


From the browser you can use the following codes to do an AJAX Post. Notice the I purposely pass dummy data to the other end. Somehow it returned text instead of JSON if i do not pass dummy data.

You can do some debugging by displaying return "data". If you can see the content of your data as shown example above, then you might not be returning JSON data type - meaning u cannot manipulate it using $each.

Example below also shows you how u can access individual record.

$.post("getExploreRecent.php", {"name" : "dummy"} , function(data)
{

//alert(data);
//alert(data.items[0].profileInfo);

$.each(data.items ,function(i,item) {
alert(item.profileInfo);
});


}, "json");


That's it. Pretty straight forward.

Comments

Popular posts from this blog

The specified initialization vector (IV) does not match the block size for this algorithm