我正在尝试使用AngularJS $ http。
在我的api请求中添加一些自定义标头
buildHttpConfig (url, options) {
var config = angular.extend({
method: 'GET',
headers: {}
}, options || {});
config.headers['Content-Type'] = 'application/json; charset=UTF-8';
config.headers['X-MyCustomHeader'] = {
"instanceId": "abc",
"platform": "Web",
"appId": "efg"
};
config.headers.Accept = 'application/json';
config.url = url;
return config;
}
getDocumentsToPresent (): ng.IPromise<string> {
var url = 'https://localhost:8080/services/xyz/myXYZ';
var deferred = this.$q.defer();
this.$http(this.buildHttpConfig(url, {method: 'GET'})).then(function (data: any) {
deferred.resolve(data);
}, function (err) {
deferred.reject(err);
});
return deferred.promise;
}
每次发出此请求时,自定义标头会自动添加到“Access-Control-Request-Headers”中。有没有办法在全局http范围中添加自定义标头?感谢。
答案 0 :(得分:0)
烨:
app.config(function ($httpProvider) {
$httpProvider.defaults.headers
.common['X-Requested-With'] = 'XMLHttpRequest';
});
我添加到我的应用程序中的那个,我提出了每个ajax请求。将X-Requested-With
更改为您需要的任何内容。