我想在我的角度Js应用程序中实现Ajax调用的全局错误处理。我认为$ httpprovider只适用于$ http请求。请指导我实现这一目标。我如何编写拦截器来捕获Ajax调用的失败。我的主要目标是捕获Ajax调用失败到谷歌分析。
答案 0 :(得分:0)
在javascripts try-catch中写一个ajax调用
http://www.w3schools.com/js/js_errors.asp
答案 1 :(得分:0)
您可以尝试收听window.onerror
事件,但它会捕获所有js错误,因此您需要对其进行过滤。
window.onerror = function(msg, url, line, col, error) {
// Note that col & error are new to the HTML 5 spec and may not be
// supported in every browser. It worked for me in Chrome.
var extra = !col ? '' : '\ncolumn: ' + col;
extra += !error ? '' : '\nerror: ' + error;
// You can view the information in an alert to see things working like this:
alert("Error: " + msg + "\nurl: " + url + "\nline: " + line + extra);
// TODO: Report this error via ajax so you can keep track
// of what pages have JS issues
var suppressErrorAlert = true;
// If you return true, then error alerts (like in older versions of
// Internet Explorer) will be suppressed.
return suppressErrorAlert;
};
您可以在this question中了解更多相关信息。