角:$ HTTP 404错误处理 [英] Angular: $http 404 error handling

查看:176
本文介绍了角:$ HTTP 404错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2 NG-包括HTML。考虑一下,如果一个NG-包括SRC是不是在服务器present。截至目前它只是加载空白的HTML并在浏览器控制台它会说找不到HTTP-404文件。

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.

因此​​,在这种情况下,我想加载默认的错误页面(这是在服务器present)到只有特定的分区,即有一半显示默认错误页和其他与适当的分区,其通过nginclude装。

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.

我的逻辑是,我使用HTTP拦截器,在那里我拦截所有的HTTP calls.whenever 404发生了,我想返回必须加载到仅在div默认的错误页面。所以它像嘲讽一个正确的HTTP调用,但是发送错误页面,而不是,我以为必须加载到适当的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.

但这个心不是happeneing :)。我试着用默认window.load('')。但随后它加载页面的顶部,是跨页present。

But this isnt happeneing :). I tried with default window.load(''). But then it loads on top of the page and is present across pages.

或者我应该捕捉DIV ID(如果又如何?),然后就达ID加载默认的错误HTML?

Or should i capture the div id (if so how?) and then do tat id load the Default Error HTML?

需要在这个你的思想。

推荐答案

要处理这种情况下,你可以使用HTTP拦截器(在这里找到的文档:的$http )。

To handle this kind of situations you could use http interceptors (find the docs here: $http).

拦截器已经赶上了404响应,从您的服务器加载页面404.html和
它设置为数据的与沿初始反应的状态的code 200。

Interceptor has to catch the 404 response, load the 404.html page from your server and set it as a data for the initial response along with the status code 200.

我创建了一个项目,显示了如何解决这个问题。

I've created a project that shows how to solve it.

存储库:<一href=\"https://github.com/matys84pl/angularjs-nginclude-handling-404/\">https://github.com/matys84pl/angularjs-nginclude-handling-404/

以在<一细看href=\"https://github.com/matys84pl/angularjs-nginclude-handling-404/blob/master/www/js/app/main.js\">main.js文件中。

这篇关于角:$ HTTP 404错误处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆