0

我使用下面的代码来处理 ajax 调用错误。我想添加该行的行号'$('body').append(...'以便能够回显它。行号是指我的 .js 文件中的行号。想知道是否有可能获得实际的行号?预先感谢您的回复。干杯。马克

$.ajax({
    type: "POST",
    url: "myfile.php",
    error: function(jqXHR, textStatus, errorThrown) {

        $('body').append('aj.ERROR! [' + jqXHR.status + ' : ' + errorThrown + ']');
    },
    success: function(data) {
        // somecode
    }
});​
4

1 回答 1

6

我知道公开当前行号的唯一方法是使用window.onerror具有以下签名的事件处理程序:

window.onerror = function(msg, url, line) { ... }

所以理论上你可以在你的代码中触发一个真正的错误(throw?),然后在错误处理程序中进行附加,例如:

window.onerror = function(msg, url, line) {
    $('body').append(msg + ' at ' + url + ':' + line);
};

$.ajax({
    type: "POST",
    url: "myfile.php",
    error: function(jqXHR, textStatus, errorThrown) {
        throw 'aj.ERROR! [' + jqXHR.status + ' : ' + errorThrown + ']';
    },
    success: function(data) {
        // somecode
    }
});​

编辑它的工作原理(至少在 Chrome 中......) - http://jsfiddle.net/alnitak/gLzY2/

于 2012-05-11T12:47:08.083 回答