$ routeProvider:与templateUrl要求的X请求,使用包头包头、routeProvider、templateUrl

2023-09-11 00:47:43 作者:你是我在青春里辗转的歌

jQuery.ajax()通常发送 X-要求 - 以头与 XMLHtt prequest 设置为内容。在服务器上(在PHP),我一般通过试验检测这样的:

jQuery.ajax() normally sends an X-Requested-With header with XMLHttpRequest set as the content. On the server (in PHP), I usually detect this by testing:

$ is_ajax = $ _ SERVER ['HTTP_X_REQUESTED_WITH'] ===XMLHtt prequest

在 AngularJS ,常用于 $ routeProvider.when发现(),你可以从服务器获取一个模板 templateUrl:/路/上/服务器

In AngularJS, commonly found in $routeProvider.when(), you can obtain a template from the server with templateUrl: '/path/on/server'.

我的问题是,是 templateUrl 请求似乎的没有的设置 X-请求─随着头,所以没有办法从任何其他类型的请求区分 templateUrl 的要求。

My problem is that templateUrl requests seem to not set X-Requested-With headers, so there's no way to distinguish templateUrl requests from any other type of request.

有没有什么办法让 $ routeProvider 发送 X-要求 - 以 XMLHtt prequest

Is there any way to get $routeProvider to send X-Requested-With as XMLHttpRequest?

参考:的

$ routeProvider文档 - (搜索 templateUrl

jQuery的jqXHR - 看 jqXHR.setRequestHeader(的X请求,并以,XMLHtt prequest)

采用了棱角分明 V1.1.5 通过谷歌CDN

更新:的我找到了actual提交角的地方开发商拆除 X-要求 - 以 $ http.get 。不知道为什么他们会怎么做呢?

Update: I found the actual commit where Angular developers removed X-Requested-With from $http.get. Wonder why they would do that?

更新:的https://github.com/angular/angular.js/issues/1004 - 讨论去除头,其中

Update: https://github.com/angular/angular.js/issues/1004 - discussion where header was removed.

推荐答案

提示的帽子为 Zerot Freenode的的的#angularjs

Tip of the hat to Zerot in FreeNode's #angularjs

app.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
    $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
    $routeProvider.when('/', {
        templateUrl: '/path/on/server',
        controller: 'Ctrl'
    });
});

编辑:更具体,这是你需要的地方就行:

to be more specific, this is the line you need somewhere:

$ httpProvider.defaults.headers.common ['X-要求,随着'] ='XMLHtt prequest';

 
精彩推荐
图片推荐