Since you are using jQuery, see .getJSON()
The way to use .getJSON() :
jQuery.getJSON( url, [ data ], [ callback(data, textStatus) ] )
url is, of course, the url from which you get the data. [ data ] - this is the material that you send to the server. [ callback(data, textStatus) ] is a function that processes data , returning the server from . You can usually leave the second argument to textStatus . The returned data is understood as JSON. .getJSON() is a shorthand for calling .ajax() , which sets the JSON data.
So, in your case, it will be something like this (note that I changed the JSON returning from the server to response ... this is a less confusing nomenclature in your case than using data , since you have data in your JSON ):
$.getJSON(base_url+'/ajax/get_itema/'+item_id+'/0/3/true', function(response) { ... });
So, to restore things from response we simply access them using point and square notation. To get the first set of data :
response.data[0].title \\ <== "Yeah Lets Go!" response.data[0].path \\ <== "http://domain.com/yeah"
The above example looks in response , which is our JSON object. Then he looks at the first data elment (there are 3) and selects title in the first line, and path in the second.
Since you are using jQuery, you can use .each() to .each() over your 3 data , like this:
$.each(response.data, function(index, value) { $("body").append('<a href="' + value.path + '">' + value.title + '</a>'); $("body").append('<p class="date">Created: ' + value.created_formated + '</p><br />'); });
.each() safe loop over a set of elements. The first argument to .each() is the object you want to .each() over. This response.data not just response . This is because we want to look at response.data[0] , response.data[1] and response.data[2] . The second argument to .each() is a callback function or what we want to do with each of the elements we iterate. Inside the callback function, the first argument is automatically the index of the element (0, 1, or 2 in your case). The second argument is the value of the element. In your case, this is a separate object: response.data[0] , response.data[1] and response.data[2] respectively. We can use point notation to extract the things that we want to get directly from these objects. In the above example, we are accessing .path . .title and .created_formated from each of value s.
This will do all your function:
$.getJSON(base_url+'/ajax/get_itema/'+item_id+'/0/3/true', function(response) { $.each(response.data, function(index, value) { $("body").append('<a href="' + value.path + '">' + value.title + '</a>'); $("body").append('<p class="date">Created: ' + value.created_formated + '</p><br />'); }); });
Of course, you probably want to add an answer to (a) a specific element / s.
Here is some good information when using .getJSON() to access multidimensional JSON data from another SO question.
Here is some general JSON information in Javascript .
Note:
You need commas between your braces!
You have:
...p:\/\/domain.com\/yeah"}{"id":"4242","title"...
You need:
...p:\/\/domain.com\/yeah"}, {"id":"4242","title"...