Angular 对后端API的角度客户端请求无法通过访问控制检查
我有一个angular 6客户端,它向一个单独的ASP.NET核心API发送请求,在startup.cs文件中有以下配置: 在配置服务方法中:Angular 对后端API的角度客户端请求无法通过访问控制检查,angular,asp.net-core,http-status-code-502,Angular,Asp.net Core,Http Status Code 502,我有一个angular 6客户端,它向一个单独的ASP.NET核心API发送请求,在startup.cs文件中有以下配置: 在配置服务方法中: services.AddCors(options => { options.AddPolicy( "MyPolicy", builder => { builder.AllowAnyOrigin() .AllowAnyMethod()
services.AddCors(options =>
{
options.AddPolicy(
"MyPolicy",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// called before UseMvc
app.UseCors("MyPolicy");
在配置方法中:
services.AddCors(options =>
{
options.AddPolicy(
"MyPolicy",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
});
// called before UseMvc
app.UseCors("MyPolicy");
在本地测试时,调用工作正常。当在测试机上部署并通过其ip和端口访问angular客户端时,请求给出:
Failed to load http://xxxIPxx:xxxPortxxx/xxxURLxxx: Response to
preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://xxxIPxx:xxxPortxxx' is therefore not allowed
access. The response had HTTP status code 502.
(这是Google Chrome显示的错误消息)
我可能遗漏了什么?当从应用程序触发请求时,应该包括
访问控制允许原点
标题
axios的快速示例:
从“axios”导入axios
const Http = axios.create({
baseURL: 'https://example.com/api',
headers: { 'Access-Control-Allow-Origin': '*' }
});
export default Http;