$ routeProvider:与templateUrl要求的X请求,使用包头 [英] $routeProvider: templateUrl requests with X-Requested-With header

查看:201
本文介绍了$ routeProvider:与templateUrl要求的X请求,使用包头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

参考:

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

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

更新:的我找到了<一href="http://gitelephant.cy$p$psslab.net/angular-js/commit/3a75b1124d062f64093a90b26630938558909e8d">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?

更新:的<一个href="https://github.com/angular/angular.js/issues/1004">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';

这篇关于$ routeProvider:与templateUrl要求的X请求,使用包头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆