访问标题数据和处理错误codeS里角的http请求 [英] Accessing header data and treating the error codes in Angular http requests

查看:142
本文介绍了访问标题数据和处理错误codeS里角的http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我如何处理我的角路由。我有一对夫妇,所有关于每条路线所需的数据存储文件和我遍历这样的数据。 html的请求装载模板和.action的AJAX请求。

我需要做的就是把所有的所在服务器不成功(300,400,500等)的案件。我GOOGLE了所有我想找到相关的一些信息吧,但似乎没有足够的相关或清除。究竟如何做我访问请求的报头数据?

  Application.config(['$ routeProvider','$ interpolateProvider','$ httpProvider',    功能($ routeProvider,$ interpolateProvider,$ httpProvider)
    {        $ interpolateProvider.startSymbol('<%');
        $ interpolateProvider.endSymbol('%GT;');        对于(VAR I = 0; I< applicationRoutes.length;我++){            如果(applicationRoutes [I] .hasInitialData)
            {
                $ routeProvider.when('/'+ applicationRoutes [I]。路径+的.html',{                    templateUrl:applicationRoutes [I]。路径+的.html',
                    控制器:applicationRoutes [I] .controller +'为'+ applicationRoutes [I] .controllerAlias​​,
                    解析:{
                        initData:'初始化','$路线',函数(初始化,$路线)
                        {
                            //console.log($route.current.$$route);
                            返回Initialize.serverData('/'+ $ route.current $$ route.pathBase);
                        }]
                    },
                    pathBase:applicationRoutes [I]。路径                })否则('/ 404')。
            }
            其他
            {
                $ routeProvider.when('/'+ applicationRoutes [I]。路径+的.html',{                    templateUrl:applicationRoutes [I]。路径+的.html',
                    控制器:applicationRoutes [I] .controller +'为'+ applicationRoutes [I] .controllerAlias​​,
                    pathBase:applicationRoutes [I]。路径                })否则('/ 404')。            }        }        $ httpProvider.defaults.headers.common [X-要求,以] ='XMLHtt prequest';    }]);


解决方案

我看不到你的服务器调用,但假设你已经推出了一个服务来调用端点与$ HTTP记住,你可以使用故障处理程序作为第二个参数承诺的那么

  $ HTTP(REQ)。然后(函数(){...},函数(){...});

这意味着你可以做到这一点。

  $ HTTP(REQ)。然后(功能(someData){/ *这是成功的* /}函数(原因){/ *的原因持有它faild的原因, reason.data等* /});

This is how I handle my routing in Angular. I have a couple of files where all the needed data about each route is stored and I iterate over the data like this. .html requests are for loading templates and .action for AJAX requests.

What I need to do is treat all the cases where the server doesn't succeed (300, 400, 500 etc). I googled all I thought relevant found some info about it but nothing seems relevant or clear enough. How exactly do I access the header data of a request?

Application.config(['$routeProvider', '$interpolateProvider', '$httpProvider',

    function($routeProvider, $interpolateProvider, $httpProvider) 
    {

        $interpolateProvider.startSymbol('<%');
        $interpolateProvider.endSymbol('%>');

        for (var i=0; i < applicationRoutes.length; i++) {

            if (applicationRoutes[i].hasInitialData)
            {
                $routeProvider.when('/' + applicationRoutes[i].path + '.html', {

                    templateUrl: applicationRoutes[i].path + '.html',
                    controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias,
                    resolve: {
                        initData : ['Initialize', '$route', function(Initialize, $route)
                        {
                            //console.log($route.current.$$route);
                            return Initialize.serverData('/' + $route.current.$$route.pathBase);
                        }]
                    },
                    pathBase: applicationRoutes[i].path

                }).otherwise('/404');
            }
            else
            {
                $routeProvider.when('/' + applicationRoutes[i].path + '.html', {

                    templateUrl: applicationRoutes[i].path + '.html',
                    controller: applicationRoutes[i].controller + ' as ' + applicationRoutes[i].controllerAlias,
                    pathBase: applicationRoutes[i].path

                }).otherwise('/404');

            }

        }

        $httpProvider.defaults.headers.common["X-Requested-With"] = 'XMLHttpRequest';

    }

]);

解决方案

I cant see your server calls, but assuming you have rolled up a Service to call the endpoints with $http remember you can use a fail handler as the second parameter of the promise so

$http(req).then(function(){...}, function(){...});

meaning you can do this

    $http(req).then(function(someData){/*This is the success*/},function(reason){/*the reason holds the reason it faild, reason.data etc */});

这篇关于访问标题数据和处理错误codeS里角的http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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