开发者

Statements inside jQuery deferred functions fail silently

开发者 https://www.devze.com 2023-03-24 12:08 出处:网络
I\'m having a jQuery issue which is making debugging JavaScript more of a bitch than it already is: statements inside of a jqXHR deferred function will fail silently if they fail, and I haven\'t found

I'm having a jQuery issue which is making debugging JavaScript more of a bitch than it already is: statements inside of a jqXHR deferred function will fail silently if they fail, and I haven't found a way to capture this error. Con开发者_开发知识库sider the following example:

var xhr = $.ajax({type: "GET",
                   url: "test.php"});
xhr.done(function() { 
    a += b; 
});

Where a and b are both undefined. This should return an error, but instead all execution stops and I get no notice that anything has failed. If, on the other hand, I write the following:

function thisIsAnError() {
    a += b;
}
thisIsAnError();

it will fail as expected, logging "a is not defined" to both the Firefox error console and the Firebug console.

A Google search turned up nothing, perhaps there is some error-handling function in jQuery I've missed? Note that xhr.fail() does not capture this error because it wasn't the XHR that failed.


The $.get() function you are using has no done() method. I believe what you're wanting to attach your function to is the success() method. jsFiddle exmample of it working.

http://api.jquery.com/jQuery.get/

http://api.jquery.com/jQuery.get/#jqxhr-object


One way of debugging js (and imo the best way) is to use the firefox of chrome extension, firebug. Firefox has a more advanced plugin than the chrome extension, but both work.

Alos, .fail() is not going to work, what you're looking for is .error()

0

精彩评论

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