AngularJS指令templateUrl通过文件URL加载返回400

Chr*_*ton 6 javascript asp.net-mvc angularjs angularjs-directive

我在MVC 5布局页面中有一个基本指令,带有搜索指令.我的问题是无法加载templateUrl(400错误).如果我直接在浏览器中输入URL,我可以毫不费力地加载html页面.我无法找出加载页面的AJAX调用失败的原因.

Chrome调试器 Chrome调试器

这是Chrome中的HTML页面加载

页面加载

app.js

(function() {
     var app = angular.module("mainApp");

     app.directive("basicSearch", function() {
        return {
            templateUrl: 'app/directives/basic-search.html',
            controller: function($scope, search) {
                $scope.keyword = '';
                $scope.exact = false;
                $scope.submitSearch = function () {
                    search.searchByKeyword($scope.keyword, 0, $scope.exact);
                }
            }
        }
     });
}());
Run Code Online (Sandbox Code Playgroud)

基本-search.html

<div>
    <input type="search" ng-model="keyword" name="keyword" />
    <button type="submit" ng-click="submitSearch()"></button>
    <input type="checkbox"ng-model="exact" name="exact" />
    <label for="exact">Exact Match?</label>
</div>
Run Code Online (Sandbox Code Playgroud)

_Layout.cshtml

<html>
     <head>
     <base href="@Request.ApplicationPath" />
     <!--- JS & CS References -->
     <title></title>
     </head>
     <body ng-app="mainApp">
     <!-- Other stuff -->
     <basic-search></basic-search>
     <!-- More other stuff -->
     @RenderBody()
     </body>
</html>
Run Code Online (Sandbox Code Playgroud)

编辑 这是成功的请求(通过浏览器):好请求

这是失败的(通过Angular指令):错误的请求

Ben*_*ull 3

Chris 提到的 IE bug 是一个缓存错误,它允许在使用 Angular 的 $http 和 IE 时重新提供旧内容。

我在这里写了关于这个主题的文章:http://benjii.me/2014/07/ie-is-caching-my-angular-requests-to-asp-net-mvc/

比删除错误修复代码更好的修复方法是修复它。请改用以下内容:

//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"] = "Fri, 01 Jan 2010 00:00:00 GMT";
Run Code Online (Sandbox Code Playgroud)