Angular JS中的通用Ajax错误处理-拦截器

Generic Ajax Error Handling - interceptor in Angular JS

本文关键字:处理 错误 Ajax JS Angular      更新时间:2023-09-26

我想在我的angular Js应用程序中实现Ajax调用的全局错误处理。我认为$httpprovider只适用于$http请求。请指导我实现这一目标。如何编写用于捕获Ajax调用失败的拦截器。我的主要目标是捕捉Ajax调用google Analytics失败的情况。

在javascript中编写ajax调用try-catch

http://www.w3schools.com/js/js_errors.asp

您可以尝试侦听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;
};

你可以在这个问题中阅读更多关于它的内容。