RegEdit
RegEdit

Reputation: 2162

How to handle error message from php to jQuery ajax error handler?

In the stub code below, when the error: method is invoked, the "errorThrown" variable just returns "object Object".

How can I get it to print out the actual text?

jQuery.ajax
({
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: myURL,

    success: function(data)
    {       
        if(data['response'] === undefined){
            this.error('No data returned');
        }

        //success code goes here                        
    },

    error: function(errorThrown)
    {
        result += errorThrown;
alert('The error was: '+errorThrown);
        return;
    }
}); 

Upvotes: 1

Views: 7833

Answers (2)

RoccoC5
RoccoC5

Reputation: 4213

The first parameter passed to the jQuery ajax error function is of type jqXHR (XMLHttpRequest in jQuery 1.4.x. http://api.jquery.com/jQuery.ajax/#jqXHR

The response will be contained in the responseText property:

error: function(errorThrown)
{
    result += errorThrown.responseText;
    alert('The error was: '+errorThrown.responseText);
    return;
}

Upvotes: 1

Paul
Paul

Reputation: 141829

The error function receives three arguments. The first is a jQueryXmlHttpRequest Object, the second and third are probably useful to you:

error: function(jqXHR, textStatus, errorThrown){
     alert('Error Message: '+textStatus);
     alert('HTTP Error: '+errorThrown);
}

Upvotes: 6

Related Questions