JSON in an AJAX request
        Posted  
        
            by Josh K
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Josh K
        
        
        
        Published on 2010-06-11T21:34:43Z
        Indexed on 
            2010/06/11
            22:03 UTC
        
        
        Read the original article
        Hit count: 370
        
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");
    }
});
© Stack Overflow or respective owner