我正在尝试使用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
});
Run Code Online (Sandbox Code Playgroud)
预检OPTIONS请求获取200 OK,但后续POST收到400 Bad Request响应.查看Chrome调试窗口中的跟踪,我没有看到Content-Type: application/x-www-form-urlencoded; charset=UTF-8POST 的标题.我假设这就是服务器返回错误请求响应的原因.
我正在设置一些我从上面的代码中省略的其他自定义标题,并且它们正在被发送并显示正常.
我还要提一下,我可以使用Chrome的Advanced Rest Client应用程序发出此请求并收到正确的回复.(访问令牌)
我也试过做一个直接的XMLHttpRequest(),但我得到了同样的错误.
有关为什么我的Content-Type标头没有被设置的任何见解?