1

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;
    }
}); 
1
  • use a Javascript Debugger and set a breakpoint in the error function. Then inspect the object coming back to see where the error text you want to display is located. Commented Aug 15, 2011 at 18:43

2 Answers 2

6

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

errorThrown.name, errorThrown.message, errorThrown.stack hold the error strings. alert(errorThrown.stack) should be enough to track down issues that may surface.
errorThrown is just a string. then how could we get stack from errorThrown.
1

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.