javascript - Angular 7:对预检请求的响应未通过访问控制检查:请求中不存在“Access-Control-Allow-Origin”标头
问题描述
我必须用 CURD 操作实现一个角度应用程序。API 已经托管在 AWS 中,这与 Postman 配合得很好。
但是我的角度应用程序越来越
从源“ http://localhost:4200 ”访问位于“ https://acp56df5alc.execute-api.us-east-1.amazonaws.com/ams/getmember ”的 XMLHttpRequest已被 CORS 策略阻止:对预检的响应请求未通过访问控制检查:请求的资源上不存在“Access-Control-Allow-Origin”标头。
我的代码如下所示,
http_GET(actionUrl: string): Observable<any> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE',
'key': 'x-api-key',
'value': 'NNctr6Tjrw9794gFXf3fi6zWBZ78j6Gv3UCb3y0x',
})
};
return this.http.get<any>(this.baseUrl + actionUrl, httpOptions).pipe(
(response => {
return response;
}));
}
我已经努力解决这个问题。但需要一些帮助
解决方案
我有同样的cors
问题,并尝试了所有建议的设置方式,Access-Control-Allow-Origin *
但没有成功。
后来发现两个问题:
data format
我通过请求发送的POST
格式不正确。- 服务器无法处理从发布请求接收到的空参数。
原始请求:
return this.http.post(API_URL + 'customer/login',
{email: email, password: password},{ headers: headers}
)
在我使用包裹发布数据后工作JSON.stringify()
return this.http.post(API_URL + 'customer/login',
JSON.stringify({email: email, password: password}),{ headers: headers}
)
推荐阅读
- node.js - 在后台模式下从 Nodejs 执行 VBS(任务计划程序或 Windows 服务)
- r - 忽略 R 中的缺失变量
- java - 如何在代理类上获取方法注释?
- c# - 异常代码 0xe0434352 崩溃 w3wp.exe | 点网运行
- mysql - 使用黑洞引擎的mysql主主复制
- docker - 如何使tomcat(url = ip:port)自动转到我的应用程序而不是根文件
- postgresql - 将字段上的值转换为新值以作为键加入 PostgreSQL
- c# - 为什么每次使用此身份验证过程都必须重新登录
- mysql - 如何根据 ID 从另一个表的单个字段中获取每一行的所有结果
- html - 如何修复'$'modal bootstrap未定义?