Angular $ http:预检的响应具有无效的HTTP状态代码403 [英] Angular $http: Response for preflight has invalid HTTP status code 403
问题描述
我正在编写一个角度应用程序,我正在尝试使用 $ http
服务连接到我们的API。
I'm writing an angular application, and i'm trying to connect to our API using the $http
service.
$http.post('https://backend-test/***/v001/login', {'userName': 'admin', 'password': 'passtest'}, {headers: {'Accept': 'application/json', 'Content-Type': 'application/json'}}).then(function success(response) {
console.log(response);
});
但是,我一直收到此错误:
However, i keep on getting this error:
XMLHttpRequest无法加载
https:// backend-test / * ** / V001 /登录。对于预检的响应
具有无效的HTTP状态代码403
XMLHttpRequest cannot load https://backend-test/***/v001/login. Response for preflight has invalid HTTP status code 403
此外,它似乎发送OPTIONS请求而不是POST请求:
Also it seems to send an OPTIONS request instead of a POST request:
请求方法:OPTIONS
Request Method:OPTIONS
奇怪的是,当我使用这个工具时它工作正常:
The strange thing is that it is working correctly when i use this tool:
有什么想法吗?
推荐答案
我遇到了同样的问题。我的后端是一个tomcat服务器。我通过配置web.xml解决了这个问题:
I had the same problem. My backend for this is a tomcat server. I solved it by configuring web.xml like this:
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>org.apache.catalina.filters.CorsFilter</filter-class>
<init-param>
<param-name>cors.allowed.headers</param-name>
<param-value>Content-Type,X-Requested-With,accept,Origin,Access-Control-Request-Method,Access-Control-Request-Headers,Authorization</param-value>
</init-param>
</filter>
这篇关于Angular $ http:预检的响应具有无效的HTTP状态代码403的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!