【发布时间】:2014-09-13 17:46:27
【问题描述】:
我正在尝试使用带有以下代码的 Angular $http 发出跨域 POST 请求。
//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
});
预检 OPTIONS 请求收到 200 OK,但随后的 POST 收到 400 Bad Request 响应。查看 Chrome 调试窗口中的跟踪,我没有看到 POST 的 Content-Type: application/x-www-form-urlencoded; charset=UTF-8 标头。我认为这就是服务器返回错误请求响应的原因。
我正在设置一些我在上面的代码中省略的其他自定义标头,并且它们正在被发送和显示。
我还应该提到,我可以使用 Chrome 的 Advanced Rest Client 应用程序发出此请求并收到正确的响应。 (访问令牌)
我也尝试过直接执行 XMLHttpRequest(),但我得到了同样的错误。
任何关于为什么我的 Content-Type 标头没有被设置的见解?
【问题讨论】:
标签: angularjs xmlhttprequest http-headers