这就是我在 Angular 中处理路由的方式。我有几个文件,其中存储了每条路线所需的所有数据,我会像这样遍历这些数据。 .html 请求用于加载模板,.action 用于 AJAX 请求。
我需要做的是处理服务器不成功的所有情况(300、400、500 等)。我用谷歌搜索了所有我认为相关的信息,找到了一些关于它的信息,但似乎没有什么相关或不够清楚。我究竟如何访问请求的 header 数据?
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';
}
]);
最佳答案
我看不到你的服务器调用,但假设你已经汇总了一个服务来调用带有 $http 的端点,请记住你可以使用失败处理程序作为 promise 的第二个参数,所以
$http(req).then(function(){...}, function(){...});
意味着你可以做到这一点
$http(req).then(function(someData){/*This is the success*/},function(reason){/*the reason holds the reason it faild, reason.data etc */});
关于javascript - 访问 header 数据并处理 Angular http 请求中的错误代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32520990/