我必须从我的网站向第三方域/服务器进行网络服务调用。 当我使用 jQuery Ajax by Post 方法和 content-type:text/plain 进行此调用时,它工作正常。
但是当我将其更改为内容类型:text/xml 时,它抛出:
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
即使它在第三方服务器上设置为允许访问我们的网站。我们在使用 content-type:text/plain 进行调用时获取此 header 。
我们还在第三方服务器上添加了以下内容。
Access-Control-Allow-Methods : Get , Post , Options ,PUT
Access-Control-Allow-Headers: Authorization,origin, content-type, accept
请告诉我飞行前请求没有得到“Access-Control-Allow-Origin”响应的原因是什么?
最佳答案
您的脚本适用于文本/纯文本的原因是因为它是一个简单的请求。如果你看this answer ,您可以看到您的文本/纯文本请求符合简单请求的要求。但是,当您将内容类型更改为 text/xml 时,它会将其更改为“非简单”请求。
为了使您的“非简单”请求发挥作用,您需要了解如何提出飞行前请求。 This website在“处理不太简单的请求”下解释了如何做到这一点。
更新
请注意:
Access-Control-Allow-Methods
对强制转换敏感(全部大写),您无需列出用于简单请求的任何方法(GET、HEAD、POST)。 -source
Access-Control-Allow-Methods: OPTIONS, PUT
Access-Control-Allow-Headers: Authorization, Origin, Content-Type, Accept
Firefox doesn't include an Origin header on same-origin requests. But Chrome and Safari include an Origin header on same-origin POST/PUT/DELETE requests (same-origin GET requests will not have an Origin header).
有没有可能来源相同?
您的 caching 可能有问题吗? ?
确保您拥有这些 settings对于您的 jquery ajax 调用:
crossDomain: true // Will force a cross domain request
cache: false
关于jquery - 与 Post 进行交叉调用,但在飞行前失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36369982/