I have a page that submits some data via AJAX to another page where it is processed and returned back using json_encode PHP function.
On my original page, in AJAX function, on success I get the data back and put it in alert to test like this:
success: function(data){
alert(data);
}
The alert outputs in the following format:
{"id":2,"item":"my text string"}
No I need to grab each part of this object and do something with them using jQuery. How do I get them? I tried: data-> but it breaks my JS code… a bit confused.
First, ensure the data you get are parsed as json by jQuery, using the
dataTypesetting.Then use
data.idordata['id']to read the id parameterThe
data['id']construct is useful when your data are for example{"some name":9}.