角度导航在获取模板时给出了“错误请求”

时间:2014-06-24 12:46:44

标签: angularjs routing navigation

我尝试将路由添加到我的新应用程序中,这是我第一次使用Angular。

我有如下所述的文件或文件片段。

app.js

'use strict';


var app = angular.module('myApplication', ['ngResource', 'ngRoute']);

app.config(['$httpProvider', function ($httpProvider) {
    //initialize get if not there
    if (!$httpProvider.defaults.headers.get) {
        $httpProvider.defaults.headers.get = {};
    }
    //disable IE ajax request caching
    $httpProvider.defaults.headers.get['If-Modified-Since'] = '0';
}]);

// Setting up clientside routes.
app.config(['$routeProvider', function ($routeProvider) {
    $routeProvider
        .when(
            '/account/details/:name', {
                templateUrl: 'templates/accountDetails.html',
                controller: 'accountDetailsController'
            })
        .when(
            '/account', {
                templateUrl: 'templates/accountOverview.html',
                controller: 'accountController'
            })
        .when(
            '/temp', {
                templateUrl: 'templates/temp.html',
                controller: 'noneController'
            })
        .otherwise({
            redirectTo: '/account'
        });
}]);

_Layout.cshtml

<!DOCTYPE html>
<html lang="en" ng-app="myApplication">
<head>
  ....
</head>
<body>
  @RenderBody()
</body>
</html>

Index.cshtml

@{
    ViewBag.Title = "Index";
}

<div ng-controller="AccountsController">
    <a href="#/account/details/jack">Jack</a>
<div>

<div ng-view />

controllers.js

'use strict';


var app = angular.module('myApplication');


app.controller('accountsController', ['$scope', 'dataService', function ($scope, dataService) {
        // Add some data retrieval here.
    }]);


app.controller('accountDetailsController', ['$scope', '$routeParams', 'dataService', function ($scope, $routeParams, dataService) {
        // Add some data retrieval here.
}]);

当我打开应用程序时,我看到有人要求检索&#39; templates / accountOverview.html&#39;像预期的那样,由于&#39;否则&#39;在$ routeprovider的配置中。 此请求使用的URL有效,我可以在浏览器中打开此URL以打开该文件。但是当Angular打开文件时,Angular会收到一个&#39; 400(错误请求)&#39;。

“接受”的请求之间存在一个很大的区别。头。  浏览器请求:接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp, / ; q = 0.8  Angular请求:接受:application / json,text / plain, /

这里发生了什么?为什么Angular会为其模板请求json或纯文本以及为什么我的服务器使用HTTP_400进行响应?我该如何解决这个问题?

现在正在寻找一段时间,但感觉我是唯一有这个问题的人:(

1 个答案:

答案 0 :(得分:3)

显然,以下代码行是我的问题。

//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = '0';

(此行的来源由另一个问题触发:Angular IE Caching issue for $http

更好的线条

//disable IE ajax request caching
$httpProvider.defaults.headers.get['If-Modified-Since'] = 'Sat, 01 Jan 2000 00:00:00 GMT';

或可能

$httpProvider.defaults.headers.get['Cache-Control'] = 'no-cache';
$httpProvider.defaults.headers.get['Pragma'] = 'no-cache';