与Post一起使用交叉呼叫但在飞行前无法通过 [英] Cross call working with Post but failing with pre-flight

查看:127
本文介绍了与Post一起使用交叉呼叫但在飞行前无法通过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须从我的网站向第三方域/服务器进行Web服务呼叫. 当我使用content-type:text/plain的jQuery Ajax by Post方法进行此调用时,它运行良好.

I have to make web service call from my websites to thirdparty domain/server. While I am making this call using jQuery Ajax by Post method with content-type:text/plain and it is working fine.

但是当我将其更改为content-type:text/xml时,它会抛出:

But while I am changing it to content-type: text/xml it is throwing:

对预检请求的响应未通过访问控制检查:否 请求中存在"Access-Control-Allow-Origin"标头 资源.

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进行调用时,我们将获得此标头.

Even it set on thirdparty server to allow access to our website. And we are getting this header while making call with content-type:text/plain.

我们还在第三方服务器上添加了以下内容.

We have also added following on Thirdparty server.

Access-Control-Allow-Methods : Get , Post , Options ,PUT

Access-Control-Allow-Headers: Authorization,origin, content-type, accept

请让我知道飞行前请求未获得"Access-Control-Allow-Origin"作为响应的原因是什么?

Please let me know what could be the reason that pre-flight request is not getting 'Access-Control-Allow-Origin' in response?

推荐答案

content-type:text/plaincontent-type: text/xml之间的区别是:"text/xml"需要预检",而"text/plain"则不需要.

The difference between content-type:text/plain and content-type: text/xml is: "text/xml" requires "preflight" but "text/plain" does not.

来自 MDN :

尤其是在以下情况下,请求将被预检:

In particular, a request is preflighted if:

它使用GET,HEAD或POST以外的方法.另外,如果使用POST 发送具有Content-Type以外的Content-Type的请求数据 application/x-www-form-urlencoded,multipart/form-data或text/plain, 例如如果POST请求使用以下命令向服务器发送XML负载 application/xml或text/xml,则该请求被预检.

It uses methods other than GET, HEAD or POST. Also, if POST is used to send request data with a Content-Type other than application/x-www-form-urlencoded, multipart/form-data, or text/plain, e.g. if the POST request sends an XML payload to the server using application/xml or text/xml, then the request is preflighted.

一些可能导致预检请求失败的原因:

Some potential reasons those can cause the fail of a preflight request:

  1. 服务器未启用CORS.搜索如何为您的服务器技术启用CORS.
  2. 服务器不使用文本/纯文本"之外的其他请求.例如; Spring有一个Access-Control-Allow-Credentials: true标头.再次来自 MDN .
  1. CORS is not enabled by server. Search how to enable CORS for your server technology.
  2. Server does not consume a request other than "text/plain". For example; Spring has a consume option that defines which content-type is acceptable.
  3. There is an "Authorization" header in your post. If you are sending requests with credentials, you should add Access-Control-Allow-Credentials: true header also. Again from MDN.

这篇关于与Post一起使用交叉呼叫但在飞行前无法通过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆