简体   繁体   English

带标题的帖子请求错误

[英]error for post request with headers

I am sending ajax request with headers in it. 我正在发送带有标头的ajax请求。 and getting error like so Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 并收到类似这样的错误,对预检请求的响应未通过访问控制检查:所请求的资源上没有'Access-Control-Allow-Origin'标头。 Origin. 起源。 I enabled header in my mvc app like so but same error is coming.and idea to fix that.... 我像这样在我的mvc应用程序中启用了标头,但即将出现相同的错误。

<customHeaders>
        <remove name="Server" />
        <remove name="X-Powered-By" />
        <remove name="X-AspNet-Version" />
        <remove name="Access-Control-Allow-Headers"/>
        <add    name="Access-Control-Allow-Headers" value="Origin, X-Requested-With, Content-Type, Accept,Token" />
        <add    name="Access-Control-Expose-Headers" value="ETag, Link, X-RateLimit-Limit, X-RateLimit-Remaining, X-Exceptionless-Client" />
        <remove name="Access-Control-Allow-Origin"/>
      </customHeaders>

The problem is that you have a different URL between Origin and Destiny. 问题在于,起源和命运之间的URL不同。

Change the Web.config of the Destiny to have the following lines: 将“命运”的Web.config更改为以下几行:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
     <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>
    </configuration>

Just be careful because anyone will be able to access your resource (*). 请小心,因为任何人都可以访问您的资源(*)。 If you want, you can write the URL origin. 如果需要,可以编写URL来源。

Another option to solve this problem would be enable CORS: https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api 解决此问题的另一种方法是启用CORS: https : //docs.microsoft.com/zh-cn/aspnet/web-api/overview/security/enabling-cross-origin-requests-in-web-api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM