fetch和jquery ajax有什么区别? [英] What is the difference between fetch and jquery ajax?

查看:160
本文介绍了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('http://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:


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

  1. 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有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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