Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 对后端API的角度客户端请求无法通过访问控制检查_Angular_Asp.net Core_Http Status Code 502 - Fatal编程技术网

Angular 对后端API的角度客户端请求无法通过访问控制检查

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()

我有一个angular 6客户端,它向一个单独的ASP.NET核心API发送请求,在startup.cs文件中有以下配置:

配置服务方法中:

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;