开发者

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

开发者 https://www.devze.com 2023-03-28 10:48 出处:网络
In the stub code below, when the error: method is invoked, the \"errorThrown\" variable just returns \"object Object\".

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
({
    contentT开发者_JAVA技巧ype: "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;
    }
}); 


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


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

精彩评论

暂无评论...
验证码 换一张
取 消