[英]$http header 'Content-Type' not appending in the header - AngularJS
I'm using $http
to do a GET request and I wanted to append 'Content-Type': 'application/json'
in the header. 我正在使用$http
来执行GET请求,我想在标题中附加'Content-Type': 'application/json'
。 If I don't append this header 415 (Unsupported Media Type)
error is thrown. 如果我没有附加此标题415 (Unsupported Media Type)
错误被抛出。
I'm using Apache 2.2.13 with ProxyPass, in which I DO NOT say RequestHeader append Content-Type "application/json"
. 我正在使用带有ProxyPass的Apache 2.2.13,其中我不要说RequestHeader append Content-Type "application/json"
。 However if I put the RequestHeader
configuration in apache $http.get
works fine and the 'Content-Type'
is also appended. 但是,如果我将RequestHeader
配置放在apache $http.get
工作正常,并且还附加了'Content-Type'
。
Scenario 1 : Using $http
, trying GET request 场景1:使用$http
,尝试GET请求
Request : 要求:
$http({
method: 'GET',
headers: { 'Content-type': 'application/json'},
url: '/items/?customerId=5656'
});
Chrome Network Tab : Chrome网络标签:
Scenario 2 : Using Restangular
, trying the same GET request 场景2:使用Restangular
,尝试相同的GET请求
Request : 要求:
Restangular.setDefaultHeaders({'Content-Type': 'application/json'})
Restangular.setBaseUrl('/items')
Restangular.all('').getList({customerId: '103020'}, {'Content-Type': 'application/json'});
Chrome Network Tab : Chrome网络标签:
The other interesting part here is, when I do some mistakes on the Request Header like, 另一个有趣的部分是,当我在Request Header上做错时,比如
Restangular.setDefaultHeaders({'Contentttt-Type': 'application/json'})
and try Scenario 1, I notice the following in the Chrome Network Tab. Restangular.setDefaultHeaders({'Contentttt-Type': 'application/json'})
并尝试方案1,我在Chrome网络选项卡中注意到以下内容。
Scenario 3 : Using jQuery
ajax, trying the same GET request 场景3:使用jQuery
ajax,尝试相同的GET请求
Request : 要求:
$.ajax({
url: "/items/?customerId=103020",
type: "GET",
beforeSend: function(xhr){xhr.setRequestHeader('Content-Type', 'application/json');},
success: function() { alert('Success!'); }
});
Chrome Network Tab : Chrome网络标签:
Questions : 问题:
RequestHeader append Content-Type "application/json"
in Apache configuration? 是否有必要在Apache配置中添加RequestHeader append Content-Type "application/json"
? But if I add that I get 415 errors on POST
requests. 但是,如果我添加,我在POST
请求中得到415错误。 Content-Type
headers to its network calls on GET
? AngularJS和Restangular有什么问题,它不会将Content-Type
标题附加到GET
上的网络调用中? Versions : 版本:
Angular : 1.2.22 Angular:1.2.22
Restangular : 1.4.0 Restangular:1.4.0
On the one hand, with the $http
service, you can override or add your headers using $httpProvider
when calling the config
method on your module : 一方面,使用$http
服务,您可以在调用模块上的config
方法时使用$httpProvider
覆盖或添加标头:
module.config(function($http) {
$httpProvider.defaults.headers.get = { 'Content-Type : 'application/json' };
});
On the other hand, with the GET
method, if your API is really RESTFull, only the Accept
header should be set since you are not sending JSON
data. 另一方面,使用GET
方法,如果您的API确实是RESTFull,则只应设置Accept
标头,因为您没有发送JSON
数据。
Thus, use the above code for your PUT
/ POST
methods and force the Accept
header for the GET
method: 因此,对PUT
/ POST
方法使用上面的代码并强制GET
方法的Accept
标头:
module.config(function($http) {
$httpProvider.defaults.headers.post = { 'Content-Type : 'application/json'};
$httpProvider.defaults.headers.put = { 'Content-Type : 'application/json'};
$httpProvider.defaults.headers.get = { 'Accept : 'application/json'};
});
EDIT 1: 编辑1:
If you want to force the content-type
in a GET
request, you have to add a blank data : 如果要在GET
请求中强制使用content-type
,则必须添加空白数据:
$http({
method: 'GET',
data:'',
dataType:'json',
headers: { 'Content-type': 'application/json'},
url: '/items/?customerId=5656'
});
Setting a Content-Type header field on an HTTP request that doesn't have a request body (such as GET) is non-sense. 在没有请求主体(例如GET)的HTTP请求上设置Content-Type头字段是没有意义的。
Maybe what you really want is set "Accept"? 也许你真正想要的是设置“接受”?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.