与Post一起使用交叉呼叫但在飞行前无法通过

编程入门 行业动态 更新时间:2024-10-11 07:29:03
本文介绍了与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/plain和content-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:

  • 服务器未启用CORS.搜索如何为您的服务器技术启用CORS.
  • 服务器不使用文本/纯文本"之外的其他请求.例如; Spring有一个消费选项,用于定义可接受的内容类型.
  • 您的帖子中有一个授权"标头.如果要发送带有凭据的请求,则还应该添加Access-Control-Allow-Credentials: true标头.再次来自 MDN .
  • CORS is not enabled by server. Search how to enable CORS for your server technology.
  • Server does not consume a request other than "text/plain". For example; Spring has a consume option that defines which content-type is acceptable.
  • 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一起使用交叉呼叫但在飞行前无法通过

    本文发布于:2023-10-10 22:15:00,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1479911.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:但在   Post

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!