我在获取跨域 POST 请求以访问最新 beta 2 版本中的 Api 控制器时遇到了很多麻烦.
Chrome(和其他浏览器)吐槽:
OPTIONS http://api.hybridwebapp.com/api/values 400(错误请求)发布 http://api.hybridwebapp.com/api/values 404(未找到)
它可能与
还有错误:
来源 http://hybridwebapp.com 不允许
这完全矛盾,就好像它忽略了标题一样.
好的,我已经通过了.这一定是我遇到过的最奇怪的问题.以下是如何解决"它:
删除这个:
<http协议><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></http协议>
因为你已经有了这个:
var enableCorsAttribute = new EnableCorsAttribute("http://mydomain.com","来源、内容类型、接受",获取、放置、发布、删除、选项");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%system32driversetc):
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:
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 请求不起作用 - 选项(错误请求) - 不允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!