我必须从我的网站到第三方域/服务器进行Web服务调用。 虽然我使用jQuery Ajax by Post方法使用content-type:text / plain进行此调用,但它工作正常。
但是当我将其更改为content-type:text / xml时,它会抛出:
对预检请求的响应未通过访问控制检查:否 '访问控制允许来源'标题出现在请求的上 资源。
即使它设置在第三方服务器上也允许访问我们的网站。我们在使用content-type进行调用时获取此标题:text / plain。
我们还在Thirdparty服务器上添加了以下内容。
Access-Control-Allow-Methods : Get , Post , Options ,PUT
Access-Control-Allow-Headers: Authorization,origin, content-type, accept
请让我知道飞行前请求未获得的原因是什么?访问控制允许来源'作为回应?
答案 0 :(得分:0)
The reason your script is working for text/plain is because it is a simple request. If you look at this answer, you can see that your text/plain request fits the requirements for a simple request. However, when you change the content-type to text/xml it changes it to a "non-simple" request.
In order to make your "non-simple" request work, you will need to look at how to make a pre-flight request. This website explains how you can do that under "Handling a not-so-simple request".
Just a Note:
The Access-Control-Allow-Methods
is cast sensitive (all uppercase), and you do not need to list any methods used for a simple request (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).
Is there a possibility that the origin is the same?
Could there be an issue with your caching?
Make sure you have these settings for your jquery ajax call:
crossDomain: true // Will force a cross domain request
cache: false