且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

从源对XMLHttpRequest的访问已被CORS策略阻止:请求的资源上不存在"Access-Control-Allow-Origin"标头

更新时间:2021-12-24 03:51:33

您需要在Web Api中启用CORS.全局启用CORS的更简单,更可取的方法是将以下内容添加到web.config

You need to enable CORS in your Web Api. The easier and preferred way to enable CORS globally is to add the following into web.config

<system.webServer>
  <httpProtocol>
    <customHeaders>
      <add name="Access-Control-Allow-Origin" value="*" />
      <add name="Access-Control-Allow-Headers" value="Content-Type" />
      <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
    </customHeaders>
  </httpProtocol>
</system.webServer>

更新:

在ASP.Net核心中,我们没有web.config,而是有app.config文件.您仍然需要web.config才能添加Web配置项模板.您可以使用该方法,例如更改最大文件上传限制等.

In ASP.Net core we do not have web.config rather we have app.config file. You still need to have web.config you can add a Web configuration item template. You can use that like change the max file upload limit etc.

发布项目时会生成web.config文件.

The web.config file is generated when you are publishing the project.