Search code examples
jqueryparse-error

$.ajax() resulting in parsererror


I have the below jquery which fetches a JSON object returned by a web service, but I get a parsererror most of the times.

$.ajax({
    type: "GET",
    url: 'scripts/php/fetchProbableDrivers.php',
    dataType: 'json',
    data: {'tripId' : tripId },
    error: function(e)
    {       

        alert(JSON.stringify(e, null, 4));                                 
            },
    success: function(drivers){
            }

Can someone please help me with this?


Solution

  • I think that there is a formatting issue with your json format .
    You can check your response by changing data type to "html" and put the alert in success.. some thing like this.

    $.ajax({
    type: "GET",
    url: 'scripts/php/fetchProbableDrivers.php',
    dataType: 'html',
    data: {'tripId' : tripId },
    error: function(e)
    {       
    
        alert(JSON.stringify(e, null, 4));                                 
            },
    success: function(strDrivers){
            alert( strDrivers );
            }
    


    Then copy you response and validate your response from the site http://jsonformatter.curiousconcept.com/

    I hope that using that way you can easily figure out the formatting issue .