Content-Type头没有被设定角$ HTTP [英] Content-Type header not being set with Angular $http
问题描述
我试图让采用了棱角分明的$ HTTP具有以下code进行跨域POST请求。
I'm trying to make a cross-origin POST request using Angular $http with the following code.
//I've tried setting and removing these http config options
$http.defaults.useXDomain = true;
delete $http.defaults.headers.common['X-Requested-With'];
$http.defaults.headers.common['Content-Type'] = 'application/x-www-form-urlencoded';
//Basic request, with some private headers removed
return $http({
method: 'POST',
//withCredentials:true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
params: params,
url: url
});
在preflight OPTIONS请求得到了 200 OK
,但随后的后收到 400错误的请求
响应。看着在Chrome的调试窗口的痕迹,我没有看到一个内容类型:应用程序/ x-WWW的形式urlen codeD;字符集= UTF-8
为POST头。我想这就是为什么服务器返回一个错误的请求响应。
The preflight OPTIONS request gets a 200 OK
, but the subsequent POST receives a 400 Bad Request
response. Looking at the trace in Chrome's debug window, I do not see a Content-Type: application/x-www-form-urlencoded; charset=UTF-8
header for the POST. I assume this is why the the server is returning a Bad Request response.
我设置我省略上面的code形式的其他自定义标题,并陆续发送,显示正常。
I'm setting some other custom headers that I have omitted form the code above, and they are being sent and displayed fine.
我还要提到的是,我可以使用Chrome浏览器的高级REST客户端应用程序使这个请求,并得到正确的响应。 (访问令牌)
I should also mention that I can make this request using the Advanced Rest Client app for Chrome and receive the correct response. (An access token)
我也尝试只是在做一个直线上升XMLHtt prequest(),但我得到了同样的错误。
I have also tried just doing a straight-up XMLHttpRequest(), but I get the same errors.
这是为什么我的Content-Type头没有被设置的任何见解?
Any insight on why my Content-Type header is not being set?
推荐答案
我不知道,如果你不发送任何数据内容类型
头将被发送。添加数据
对象和尝试:
I'm not sure the Content-Type
header will be sent if you are NOT sending any data. Add a data
object and try it:
return $http({
method: 'POST',
//withCredentials:true,
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'},
data: data,
url: url
});
此外,通常使用数据后
而不是 PARAMS
(获取)。
您也可以参考此SO疑问,对如何,如果你需要转换数据一些更多的信息:<一href=\"http://stackoverflow.com/questions/11442632/how-can-i-make-angular-js-post-data-as-form-data-instead-of-a-request-payload\">How我可以让angular.js后的数据作为表格数据,而不是请求负载?
You can also refer to this SO question that has some more info on how to transform the data if you need to: How can I make angular.js post data as form data instead of a request payload?
这篇关于Content-Type头没有被设定角$ HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!