预检响应在角度发布请求上具有无效的HTTP状态代码403 [英] Response for preflight has invalid HTTP status code 403 on angular post request
问题描述
我在本地主机中有一个$ http请求,该请求正在调用某些api的url.我在执行通话时遇到错误
I am having an $http request in my localhost which is calling a url of some api. I am getting an error on executing the call
Response for preflight has invalid HTTP status code 403
我可以使用angular做任何事情,以便解决此问题吗?我有chrome的CROS插件,可以允许跨源请求
Can I do anything using angular so that I can fix this issue? I have CROS plugin of chrome to allow cross origin request
$http({
method: 'POST',
url: url,
data:data1,
headers: {
'Access-Control-Allow-Origin': '*',
}
})
推荐答案
好的,这就是我的解决方法.这都与CORS政策有关.在发布POST请求之前,Chrome正在执行预检OPTIONS请求,服务器应在实际请求之前对其进行处理和确认.现在,这真的不是我想要的那样简单的服务器.因此,在客户端重置标头可防止预检:
Ok so here's how I figured this out. It all has to do with CORS policy. Before the POST request, Chrome was doing a preflight OPTIONS request, which should be handled and acknowledged by the server prior to the actual request. Now this is really not what I wanted for such a simple server. Hence, resetting the headers client side prevents the preflight:
app.config(function ($httpProvider) {
$httpProvider.defaults.headers.common = {};
$httpProvider.defaults.headers.post = {};
$httpProvider.defaults.headers.put = {};
$httpProvider.defaults.headers.patch = {};
});
浏览器现在将直接发送POST.希望这对很多人有帮助...我的真正问题是对CORS的了解不足.
The browser will now send a POST directly. Hope this helps a lot of folks out there... My real problem was not understanding CORS enough.
链接到一个很好的解释: http://www.html5rocks.com/en/教程/cors/
Link to a great explanation: http://www.html5rocks.com/en/tutorials/cors/
对这个答案表示敬意,向我展示了方法. AngularJS POST失败:对预检的响应已无效的HTTP状态代码404
Kudos to this answer for showing me the way. AngularJS POST Fails: Response for preflight has invalid HTTP status code 404
这篇关于预检响应在角度发布请求上具有无效的HTTP状态代码403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!