且构网

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

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

更新时间:2021-12-20 03:02:48

您需要在 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.