JSON in an AJAX request
- by Josh K
I have a PHP API I'm working with that outputs everything as JSON.
I need to call one of the API methods and parse it out using an AJAX request. I am using jQuery (though it shouldn't matter).
When I make the request it errors out with a "parsererror" as the textStatus and a "Syntax Error: invalid label" when I make the request.
Simplified code:
$.ajax
({
type: "POST",
url: "http://mydomain.com/api/get/userlist/"+mid,
dataType: "json",
dataFilter: function(data, type)
{
/* Here we assume and pray */
users = eval(data);
alert(users[1].id);
},
success: function(data, textStatus, XMLHttpRequest)
{
alert(data.length); // Should be an array, yet is undefined.
},
error: function(XMLHttpRequest, textStatus, errorThrown)
{
alert(textStatus);
alert(errorThrown);
},
complete: function(XMLHttpRequest, textStatus)
{
alert("Done");
}
});