本文介绍了CORS POST请求不工作 - 选项(错误请求) - 不允许原产地的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在获取跨域POST请求时遇到了很多困难,无法在最新的beta 2版本中打开Api控制器。



Chrome(和其他浏览器)吐出:

 选项http://api.hybridwebapp.com/api/values 400(错误请求)
POST http://api.hybridwebapp.com/api/values 404(找不到)



但是错误:



来源 http://

确定我过去了。这必须是我遇到的最奇怪的问题。以下是解决问题的方法:


  1. 继续照常生活,直到突然离开目的地请求到这个域开始返回200 OK(而不是400 Bad Request),并且POST从不发生(或者至少看起来不是因为浏览器吞没了它)

  2. 意识到Fiddler的OPTIONS响应神秘地包含Access-Control-Allow-XXX的重复。

  3. 尝试从您的web.config中删除以下语句,即使您清楚记得尝试修复以前的问题,并且无法正常工作:

删除:

 < httpProtocol> 
< customHeaders>
< remove name =X-Powered-By/>
< add name =Access-Control-Allow-Originvalue =http://mydomain.com/>
< add name =Access-Control-Allow-Headersvalue =Accept,Content-Type,Origin/>
< add name =Access-Control-Allow-Methodsvalue =GET,PUT,POST,DELETE,OPTIONS/>
< / customHeaders>
< / httpProtocol>

因为你已经有这个:

  var enableCorsAttribute = new EnableCorsAttribute(http://mydomain.com,
Origin,Content-Type,Accept,
GET,PUT,POST, DELETE,OPTIONS);
config.EnableCors(enableCorsAttribute);

道德:你只需要一个。


I'm having a lot of trouble getting a cross domain POST request to hit an Api controller in the latest beta 2 release.

Chrome (and other browsers) spit out:

OPTIONS http://api.hybridwebapp.com/api/values 400 (Bad Request)
POST http://api.hybridwebapp.com/api/values 404 (Not Found)

It may be related to this issue but I have applied that workaround and several other fixes such as web.config additions here

I've been banging my head with this for a while so I created a solution to reproduce the problem exactly.

Load the web app there will be 2 buttons one for GET one for POST and the response will appear next to the button. GET works. Cannot get POST to return successfully.

I'm able to get a hint at the cause from Fiddler but it makes no sense because if you look at the response it DOES include the domain in the Access-Controll-Allow-Origin header:

There is a folder in the solution called "ConfigurationScreenshots" with a few screenshots of the IIS configuration (website bindings) and Project properties configurations to make it as easy as possible to help me :)

EDIT: Don't forget to add this entry to host file (%SystemRoot%\system32\drivers\etc):

 127.0.0.1     hybridwebapp.com  api.hybridwebapp.com

**STATUS: ** It seems that some browsers like Chrome allow me to proceed with the POST regardless of the error message in the OPTIONS response (while others like Firefox don't). But I don't consider that solved.

Look at the Fidler screenshots of the OPTIONS request it has

Access-Control-Allow-Origin: http://hybridwebapp.com

And yet the error:

The origin http://hybridwebapp.com is not allowed

That is completely contradictory it's as if it's ignoring the header.

解决方案

Ok I got past this. This has got to be the strangest issue I've ever encountered. Here's how to "solve" it:

  1. Continue on with life as usual until suddenly out of no where OPTIONS requests to this domain begin returning 200 OK (instead of 400 Bad Request) and POST never happens (or at least seems like it doesn't because the browser swallows it)
  2. Realize that Fiddler's OPTIONS response mysteriously contains duplicates for "Access-Control-Allow-XXX".
  3. Try removing the following statement from you web.config even though you clearly remember trying that to fix the previous issue and it not working:

Remove this:

    <httpProtocol>
       <customHeaders>
         <remove name="X-Powered-By" />
         <add name="Access-Control-Allow-Origin" value="http://mydomain.com" />
         <add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
         <add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />
       </customHeaders>
    </httpProtocol>

Because you already have this:

 var enableCorsAttribute = new EnableCorsAttribute("http://mydomain.com",
                                                   "Origin, Content-Type, Accept",
                                                   "GET, PUT, POST, DELETE, OPTIONS");
        config.EnableCors(enableCorsAttribute);

Moral: You only need one.

这篇关于CORS POST请求不工作 - 选项(错误请求) - 不允许原产地的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 20:53