使用fetch封装ajax_如何使用Fetch在JavaScript中进行AJAX调用
使用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
optionsparameter. Some other possible values
optionscan take -
默认情况下,该请求作为GET发送。 要发送POST / PATCH / DELETE / PUT,您可以将method属性用作
options参数的一部分。 其他一些可能的值
options可以采用-
method
: such as GET, POST, PATCHmethod
:例如GET,POST,PATCHheaders
: Headers objectheaders
:headers
头对象mode
: such ascors
,no-cors
,same-origin
mode
:例如cors
,no-cors
,same-origin
cache
: cache mode for requestcache
:请求的缓存模式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
- Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理 .
- Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理
- 如何使用JavaScript进行可靠的继承调用
- Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理
- Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理 .
- Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理
- .Ext Ajax:如何调用Ext.Ajax.request方法和使用Java Servlet进行处理
- 第三部分 02 在ASP.NET AJAX应用程序中使用JavaScript异步调用Page的方法
- 在使用了母版页的内容页后,如何在javascript中调用服务器控件值
- ArcGIS API for javascript开发笔记(六)——REST详解及如何使用REST API调用GP服务
- 如何对动态创建控件进行验证以及在Ajax环境中的使用
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(4):异步通讯层生成的客户端代理类、使用HTTP GET进行调用
- 如何使用网页开发自己的app,在网页中的按钮与自己的java代码绑定来实现打电话即javascript代码调用java代码,和java代码来调用javascript代码
- AJAX是什么?如何在JavaScript中使用?
- Flex中不使用FelxPrintJob通过ExternalInterface直接调用JavaScript利用浏览器本身的功能进行打印
- 服务器控件使用Javascript进行验证(选择地调用后台事件)
- 使用Extjs,jquery,javascript进行ajax请求实例分析
- 使用Ajax在javascript中调用后台C#函数
- 如何使用 QUnit 进行 JavaScript 单元测试
- 【JavaScript】使用Chrome进行调试(如何使用Chrome开发者工具)