I have created a JSON object in php using json_encode and am attempting to display certain values of that object in javascipt through AJAX. I have no issue receiving the response from php but am getting 'undefined' when I try to access any value of the response using JSON notation.
Code snippet:
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
var response = xmlhttp.responseText;
alert(response);
alert(response.data);
}
Output:
{"data":[{"cheese":"pork"},{"cheese":"chicken"}]} // Yes, I'm hungry right now.
undefined
EDIT: Thanks everybody for the responses. Wish I could give you all a check mark but I have selected the most helpful response.
eval
, but there's security risks of this} – Rudu Jul 20 '11 at 16:55