当我从Angular向.net core发送发布请求时收到错误

时间:2019-04-08 12:05:23

标签: .net angular angular7 core

当我从Angular向.net core发送发布请求时收到错误消息

错误消息:

CORS策略已阻止从源“ yyyy”访问“ xxxx”处的XMLHttpRequest:请求的资源上没有“ Access-Control-Allow-Origin”标头。

角侧

   add(item:AchievementsAddEdit){

    let formData: FormData = new FormData(); 
    for (const prop in item) {
      if (!item.hasOwnProperty(prop)) { continue; }
      formData.append(prop , item[prop]);
    }
    let headers = new HttpHeaders();
    //headers.append("Authorization","Bearer"+this.authService.token);
    this.httpClient.post(this.path + 'achievements/add',formData).subscribe(data=>{
        //console.log(data);
    },
    error=>{
      alert("Error Message: Fill in the required fields");
    } 

    );
  }

服务器端startup.cs ConfigureServices

  services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                    builder => builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials());
            });

服务端配置

app.UseCors("CorsPolicy");

Web配置(发布)

    <?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath=".\Unchained.API.exe" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>
<!--ProjectGuid: 14963db0-9f3a-4a6b-b384-b97dd96f61ad-->

0 个答案:

没有答案