Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

javascript - Angular: $http 404 error handling

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.

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.

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.

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

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

Need your thought in this.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

Repository: https://github.com/matys84pl/angularjs-nginclude-handling-404/

Take a closer look at the main.js file.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...