开发者

Firefox doesn't show javascript errors on AJAX 'threads'?

I have a function test_fn that's called two ways:

(a) From an onclick attribute of a button; or (b) from the success function of a jQuery AJAX call, like this:

$.ajax({
    type:  'POST',
    ...
    success: function(data) {
        test_fn();
    }
});

I've noticed that my Firefox (3.6.17 on Linux) Error Console doesn't seem to show me any javascript errors that arise in code called by the AJAX success function. Whereas it does show errors, as usual, in code that's run by a GUI action.

For example, if I have my test function test_fn defined as follows:

function test_fn() {
    blah
}

I'll see the obvious javascript error reported on the Error Console when test_fn is invoked via a click on the button, but NOT when my code does the (successful) AJAX call and calls test_fn that way.

I've looked around the web and haven't seen anything along the lines of "AJAX 'threads' don't log their javascript errors" or anything like that. A lot of my javascript is executed within an AJAX callback ... I'm wondering if this behaviour has sneaked in somewhere/somehow. I'm a bit rattled by it; when one's Javascript silently fails it's hard to track down where without an error message or lin开发者_C百科e number.

Can anyone offer any help or advice? Thanks.


Have you tried:

$.ajax({
    type:  'POST',
    ...
    success: function(data) {
        test_fn();
    },
    error: function(jqXHR, textStatus, errorThrown){
       // Aw Snap... Check textStatus maybe?
    }
});


You can wrap the whole thing in a try...catch statement and log it if there's an exception. That can get a little tedious, so you may want to make a function which returns a function which wraps a function for you:

function wrap(func) {
    return function wrapped() {
        try {
            func.apply(this, arguments);
        }catch(ex) {
            if(console&&console.error) {
                console.error(ex);
            }else{
                alert(ex);
            }
        }
    };
}

Use it like this:

$.ajax({
    ...
    success: wrap(function(data) {
        // Test it by generating an error by calling a number.
        (1)();
    })
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜