Javascript 角度:$http 404 错误处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13603078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Angular: $http 404 error handling
提问by Aki
i have an HTML with 2 ng-includes. Consider if one of the ng-include src is not present in the server. as of now it would just load blank html and in the browser console it would say http-404 file not found.
我有一个带有 2 个 ng-includes 的 HTML。考虑服务器中是否不存在 ng-include src 之一。到目前为止,它只会加载空白的 html,并且在浏览器控制台中它会说找不到 http-404 文件。
So in this case, i want to load a Default Error page (which is present in server) into that particular div only, i.e one half showing Default Error Page and other with the proper div which was loaded via nginclude.
因此,在这种情况下,我只想将默认错误页面(存在于服务器中)加载到该特定 div 中,即一半显示默认错误页面,另一半显示通过 nginclude 加载的正确 div。
My logic is, am using an http interceptor, where i intercept all http calls.whenever a 404 happens, i want to return the Default Error page which must be loaded into the div only. so its like mocking a correct http call but sending a error page instead, which i assume must be loaded to the proper div.
我的逻辑是,我正在使用 http 拦截器,在那里我拦截所有 http 调用。每当发生 404 时,我想返回必须仅加载到 div 中的默认错误页面。所以它就像模拟一个正确的 http 调用,而是发送一个错误页面,我认为必须将其加载到正确的 div 中。
But this isnt happeneing :). I tried with default window.load(''). But then it loads on top of the page and is present across pages.
但这不会发生:)。我尝试使用默认的 window.load('')。但随后它会加载到页面顶部并跨页面出现。
Or should i capture the div id (if so how?) and then do tat id load the Default Error HTML?
或者我应该捕获 div id(如果是这样,如何?)然后执行 tat id 加载默认错误 HTML?
Need your thought in this.
这需要你的想法。
回答by matys84pl
To handle this kind of situations you could use http interceptors (find the docs here: $http).
要处理这种情况,您可以使用 http 拦截器(在此处找到文档:$http)。
Interceptor has to catch the 404 response, load the 404.html page from your server and set it as a datafor the initial response along with the statuscode 200.
拦截器必须捕获 404 响应,从您的服务器加载 404.html 页面并将其设置为初始响应的数据以及状态代码 200。
I've created a project that shows how to solve it.
我创建了一个项目来展示如何解决它。
Repository: https://github.com/matys84pl/angularjs-nginclude-handling-404/
存储库:https: //github.com/matys84pl/angularjs-nginclude-handling-404/
Take a closer look at the main.jsfile.
仔细查看main.js文件。
回答by user1130072
I did something similar by passing the desired ng-include url through $http directly before populating the ng-include value.
我通过在填充 ng-include 值之前直接通过 $http 传递所需的 ng-include url 做了类似的事情。
$http({ url: url, method: "GET", cache: $templateCache}).success(function(data) {
/* there was a template for this url - set the $scope property that
* the ng-include is using
*/
$scope.templateUrl = url;
}).error(function () {
// there was not a template for this url - set the default one
$scope.templateUrl = defaultUrl;
});
The trick here is passing $templateCache in as the cache argument to $http - this means that the fetched url is stored in the same cache that ng-include uses, and so when you find a valid template and set it in the templateUrl property, ng-include does not need to fetch the template again.
这里的技巧是将 $templateCache 作为缓存参数传递给 $http - 这意味着获取的 url 存储在 ng-include 使用的相同缓存中,因此当您找到有效模板并将其设置在 templateUrl 属性中时, ng-include 不需要再次获取模板。

