从Angular JS调用Web API错误:'Access-Control-Allow-Origin'标头包含多个值,因此不允许Origin访问

问题描述 投票:2回答:1
  1. 项目清单

我在'https://example.com/api/gd/alldata'上部署了一个Web API,当我直接从浏览器URL运行它时会显示结果。但是当我从其他Angular JS应用程序调用它时,它给出了我的错误。

错误:

无法加载'https://example.com/api/gd/alldata''Access-Control-Allow-Origin'标题包含多个值'http://localhost:64000http://localhost:64000,*',但只允许一个值。因此,'http://localhost:64000'原产地不允许进入。

这是我在另一个应用程序中的新angularjs服务。

  var promise = $http({

        method: 'GET',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: "https://example.com/api/gd/alldata",
        //headers: headers, 
        headers: {
            'Access-Control-Allow-Origin': '*',
            'Content-Type': 'application/json',
        }
    })
        .then(function (response) {
            console.log(response.data)
            return response.data;
        },
        function (response) {
                return response.data;
            });

以下是我在部分链接上部署的API代码,

我在webapiconfig中启用了cors,并在startup.cs文件中输入了。

Startup.cs文件

    app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

webapiconfig.cs

 config.EnableCors();

你可以告诉我我在哪里做错了,或者我在代码中遗失或做错了什么。

c# angularjs azure asp.net-web-api cors
1个回答
3
投票

由于您在Azure上托管并从localhost(本地开发环境)进行调用,因此您必须在Azure App Service上更新以下CORS设置:

  1. 转到https://portal.azure.com上的App Service
  2. 搜索CORS(按CTRL + /)
  3. 在允许的起源下添加你的localhost:6400或更好的*
  4. 保存

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.