fetch和jquery ajax有什么区别?

编程入门 行业动态 更新时间:2024-10-27 10:28:25
本文介绍了fetch和jquery ajax有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过fetch发送一个帖子请求,但它不起作用。

I want to send a post request through fetch, but it does not work.

但如果我通过jQuery ajax这样做,它就会成功。

But if I do it through jQuery ajax, it succeeds.

我想知道两种方式的区别,如果我在这里使用fetch有什么问题:

I want to know the difference of the two way and if there is anything wrong in my use of fetch here:

fetch('localhost:8888/news',{ method:"post", data:"code=7&a=8&b=9" }).then(function(data){ data.json().then(function (json) { }

推荐答案

获取规格与不同jQuery.ajax()主要有两种方式:

Fetch specification differs from jQuery.ajax() in mainly two ways:

  • 从fetch()返回的Promise赢了即使响应是HTTP 404或500,也不会拒绝HTTP错误状态。相反,它将正常解析(ok状态设置为false),它只会拒绝网络故障或者任何事情阻止请求完成。

  • The Promise returned from fetch() won’t reject on HTTP error status even if the response is an HTTP 404 or 500. Instead, it will resolve normally (with ok status set to false), and it will only reject on network failure or if anything prevented the request from completing.

    默认情况下,fetch不会发送或接收来自服务器的任何cookie,如果站点依赖于维护用户会话,则会导致未经身份验证的请求(要发送cookie,必须设置凭证init选项)。

    By default, fetch won't send or receive any cookies from the server, resulting in unauthenticated requests if the site relies on maintaining a user session (to send cookies, the credentials init option must be set).

  • 更多推荐

    fetch和jquery ajax有什么区别?

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

    发布评论

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

    >www.elefans.com

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