使用fetch封装ajax

I will be sharing bite sized learnings about JavaScript regularly in this series. We'll cover JS fundamentals, browsers, DOM, system design, domain architecture and frameworks.

在本系列中,我将定期分享有关JavaScript的小知识。 我们将介绍JS基础知识,浏览器,DOM,系统设计,域架构和框架。

Fetch is an interface for making an AJAX request in JavaScript. It is implemented widely by modern browsers and is used to call an API.

Fetch是用于在JavaScript中发出AJAX请求的接口。 它由现代浏览器广泛实现,并用于调用API。

const promise = fetch(url, [options])

Calling fetch returns a promise, with a Response object. The promise is rejected if there is a network error, and it's resolved if there is no problem connecting to the server and the server responded a status code. This status code could be 200s, 400s or 500s.

调用fetch返回带有响应对象的Promise。 如果出现网络错误,则将拒绝诺言;如果连接到服务器没有问题,并且服务器响应了状态代码,则可以解决诺言。 此状态码可以是200s,400s或500s。

A sample FETCH request -

样本FETCH请求-

fetch(url).then(response => response.json()).catch(err => console.log(err))

The request is sent as a GET by default. To send a POST / PATCH / DELETE / PUT you can use the method property as part of options parameter. Some other possible values options can take -

默认情况下,该请求作为GET发送。 要发送POST / PATCH / DELETE / PUT,您可以将method属性用作options参数的一部分。 其他一些可能的值options可以采用-

  • method: such as GET, POST, PATCH

    method :例如GET,POST,PATCH

  • headers: Headers object

    headersheaders头对象

  • mode: such as cors, no-cors, same-origin

    mode :例如corsno-corssame-origin

  • cache: cache mode for request

    cache :请求的缓存模式

  • credentials

    credentials

  • body

    body

Check out the full list of available options here

在此处查看可用选项的完整列表

Example usage: This example demonstrates the usage of fetch to call an API and to get a list of git repositories.

用法示例:此示例演示fetch的用法,以调用API并获取git存储库列表。

const url = 'https://api.github.com/users/shrutikapoor08/repos';fetch(url).then(response => response.json()).then(repos => {const reposList = repos.map(repo => repo.name);console.log(reposList);})
.catch(err => console.log(err))

To send a POST request, here's how the method parameter can be used with async / await syntax.

要发送POST请求,以下是method参数与async / await语法一起使用的方式。

const params = {id: 123
}const response = await fetch('url', {method: 'POST',headers: {'Content-Type': 'application/json'},body: JSON.stringify(params)
});const data = await response.json();


对更多JSBytes感兴趣? 订阅新闻通讯 (Interested in more JSBytes? Sign up for the newsletter)

翻译自: https://www.freecodecamp.org/news/how-to-use-fetch-api/

使用fetch封装ajax

使用fetch封装ajax_如何使用Fetch在JavaScript中进行AJAX调用相关推荐

  1. react 版权问题_react使用fetch封装请求的方法-简单易懂

    方法有很多种,这里贴一个比较优雅又简单易懂的.用class类写法,new一个实例方法. 其中qs是包,类似于url拼接的方法,自行npm安装.我这里fetch使用的是dva的fetch,也可以自行使用 ...

  2. 使用fetch封装请求_关于如何使用Fetch API执行HTTP请求的实用ES6指南

    使用fetch封装请求 In this guide, I'll show you how to use the Fetch API (ES6+) to perform HTTP requests to ...

  3. React Native Fetch封装那点事...

    每一门语言都离不开网络请求,有自己的一套Networking Api.React Native使用的是Fetch. 今天我们来谈谈与Fetch相关的一些事情. purpose 通过这篇文章,你将了解到 ...

  4. zhr 一个简易的fetch封装

    /*** 一个简易fetch封装* author: slongzhang* date: 2022-06-13* 修改于 https://github.com/Sunny-lucking/howToBu ...

  5. fetch vue读取json文件_前端笔记——尝试理解并在JavaScript中使用Fetch()

    介绍 Fetch()方法可以发出类似于XMLHttpRequest(XHR)的网络请求.Fetch API和XMLHttpRequest之间的主要区别(也称为XHR)是Fetch API使用Promi ...

  6. h5的fetch方法_你不需要jQuery(三):新AJAX方法fetch()

    XMLHttpRequest来完成ajax有些老而过时了. fetch()能让我们完成类似 XMLHttpRequest (XHR) 提供的ajax功能.它们之间的主要区别是,Fetch API 使用 ...

  7. 12c分页查询特性FETCH FIRST ROWS,OFFSET ROWS FETCH NEXT ROW LIMIT Clause子句

    Database 12c的FETCH FIRST ROWS特性可以简化老版本中ROW_NUM()或ROWNUM的分页排序写法, 大幅节约开发花在分页查询语句上的时间. row-limiting子句用以 ...

  8. fetch git pull 切换_git fetch git pull

    解析.git文件夹 进入正题之前,我们先来看看.git文件夹 1.config配置文件 [core] repositoryformatversion = 0 filemode = true bare ...

  9. fetch 不是xhr_XMLHttpRequest和Fetch API,您认为哪种最适合Ajax?

    来台湾出差快一个星期了,相比大陆深圳,还是更喜欢内地多点,这边的天气实在不敢恭维,天天下雨,交通汽车尾气太重了,摩托车四处穿插.....休息时间还是呆在家里,写写东西,给大家分享点干货,今天来聊点数据 ...

最新文章

  1. SpringMVC Root WebApplicationContext启动流程
  2. oracle数据库基础知识总结,oracle知识点总结(一)
  3. 11gR2 Grid Infrastructure Installation prerequisites On LINUX
  4. 线上python课程一般多少钱-排名前十的python零基础编程在线网课一对一费用多少钱...
  5. win10电脑360调用不到JAVA,win7/win10系统360浏览器打不开原因及解决方法
  6. Namecheap 给域名添加ssl证书
  7. 在安卓手机中使用微信保存图片提示失败的处理
  8. 开源时代:刘韧对话任旭东崔宝秋章文嵩蒋涛
  9. django-上下文处理器
  10. Perl语言的多线程(一)
  11. 如果使用半导体RFID读写器CK-S640-AP60E读取RI-TRP-DR2B-40的UID信息
  12. API接口搜索商品列表的调用展示
  13. 自动驾驶轨迹预测论文阅读(一)Deep Learning-based Vehicle Behaviour Prediction For Autonomous Driving Applications
  14. 想借微商发家?打造千万级分销体系的方法论在这里!
  15. OCP学习——数据库整库备份恢复实验
  16. 论文:Detecting and Correcting for Label Shift with Black Box Predictors(BBSE)
  17. 计算机科学与技术的专业概论论文,计算机科学与技术专业概论论文.docx
  18. arduinorgb三色灯_Arduino教程2.2-RGB-LED灯以及PWM
  19. 基础编程题目集 函数题部分
  20. opencv保存图片小bug

热门文章

  1. 【C++ Priemr | 15】虚函数常见问题
  2. Java面试题库,java核心技术第十版下载
  3. java小程序查看器,成功拿到offer
  4. 如何使用阿里云云解析API实现动态域名解析,搭建私有服务器
  5. Appium——主从控制执行
  6. 问题总结2015/05/05
  7. 添加远程链接MySQL的权限
  8. MFC学习中遇到的小问题和解决方案
  9. 简单的Excel导出(两种方式)
  10. JS 数组 各项操作