AngularJS 指令 templateUrl returns 400 虽然文件 URL 加载
AngularJS Directive templateUrl returns 400 though file URL loads
我在 MVC 5 布局页面中有一个基本指令,其中包含一个用于搜索的指令。我的问题是无法加载 templateUrl(400 错误)。如果我直接在浏览器中输入 URL,我可以毫无困难或错误地加载 html 页面。我无法找出加载页面的 AJAX 调用失败的原因。
Chrome调试器
这是 HTML 页面加载 Chrome
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);
}
}
}
});
}());
基本-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>
_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>
编辑
这是成功的请求(通过浏览器):
这是失败的(通过 Angular 指令):
我解决了。似乎有人将以下内容添加到模块配置中以尝试解决 IE 错误(惊喜,惊喜):
//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";
我删除了它,清除了我的缓存,它工作正常。
Chris 提到的 IE 错误是一个缓存错误,在使用 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";
我在 MVC 5 布局页面中有一个基本指令,其中包含一个用于搜索的指令。我的问题是无法加载 templateUrl(400 错误)。如果我直接在浏览器中输入 URL,我可以毫无困难或错误地加载 html 页面。我无法找出加载页面的 AJAX 调用失败的原因。
Chrome调试器
这是 HTML 页面加载 Chrome
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);
}
}
}
});
}());
基本-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>
_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>
编辑
这是成功的请求(通过浏览器):
这是失败的(通过 Angular 指令):
我解决了。似乎有人将以下内容添加到模块配置中以尝试解决 IE 错误(惊喜,惊喜):
//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";
我删除了它,清除了我的缓存,它工作正常。
Chris 提到的 IE 错误是一个缓存错误,在使用 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";