本文共 4326 字,大约阅读时间需要 14 分钟。
axios 请求node
Axios is a very popular JavaScript library you can use to perform HTTP requests, that works in both Browser and platforms.
Axios是一个非常流行JavaScript库,可用于执行HTTP请求,该库在Browser和平台中均可使用。
It supports all modern browsers, including support for IE8 and higher.
它支持所有现代浏览器,包括对IE8和更高版本的支持。
It is promise-based, and this lets us write async/await code to perform requests very easily.
它是基于Promise的,这使我们可以编写异步/等待代码来非常轻松地执行请求。
Using Axios has quite a few advantages over the native :
与本地相比,使用Axios具有很多优势:
Check out this video where I create an Express server that offers a POST endpoint, and I make an Axios request to it, to post data:
观看此视频,在该视频中我将创建一个提供POST端点的Express服务器,并向其发出Axios请求以发布数据:
Axios can be installed using :
可以使用安装Axios:
npm install axios
or :
或 :
yarn add axios
or include it in your page using unpkg.com:
或使用unpkg.com将其包含在您的页面中:
< script src = "https://unpkg.com/axios/dist/axios.min.js" >
You can start an HTTP request from the axios
object:
您可以从axios
对象启动HTTP请求:
I use
foo
andbar
as random names. Enter any kind of name to replace them.我使用
foo
和bar
作为随机名称 。 输入任何名称以替换它们。
axios ({ url : 'https://dog.ceo/api/breeds/list/all' , method : 'get' , data : { foo : 'bar' }})
but for convenience, you will generally use
但是为了方便起见,您通常会使用
axios.get()
axios.get()
axios.post()
axios.post()
(like in jQuery you would use $.get()
and $.post()
instead of $.ajax()
)
(就像在jQuery中,您将使用$.get()
和$.post()
代替$.ajax()
)
Axios offers methods for all the HTTP verbs, which are less popular but still used:
Axios提供了用于所有HTTP动词的方法,这些方法不太流行,但仍在使用:
axios.delete()
axios.delete()
axios.put()
axios.put()
axios.patch()
axios.patch()
axios.options()
axios.options()
and a method to get the HTTP headers of a request, discarding the body:
以及获取请求的HTTP标头并舍弃正文的方法:
axios.head()
axios.head()
One convenient way to use Axios is to use the modern (ES2017) async/await syntax.
使用Axios的一种便捷方法是使用现代(ES2017)异步/等待语法。
This Node.js example queries the to retrieve a list of all the dogs breeds, using axios.get()
, and it counts them:
此Node.js示例使用axios.get()
查询以检索所有品种的狗的列表,并对它们进行计数:
const axios = require ( 'axios' )const getBreeds = async () => { try { return await axios . get ( 'https://dog.ceo/api/breeds/list/all' ) } catch ( error ) { console . error ( error ) }}const countBreeds = async () => { const breeds = await getBreeds () if ( breeds . data . message ) { console . log ( `Got ${ Object. entries ( breeds . data . message ). length } breeds` ) }}countBreeds ()
If you don’t want to use async/await you can use the syntax:
如果您不想使用异步/等待,则可以使用语法:
const axios = require ( 'axios' )const getBreeds = () => { try { return axios . get ( 'https://dog.ceo/api/breeds/list/all' ) } catch ( error ) { console . error ( error ) }}const countBreeds = async () => { const breeds = getBreeds () . then ( response => { if ( response . data . message ) { console . log ( `Got ${ Object. entries ( response . data . message ). length } breeds` ) } }) . catch ( error => { console . log ( error ) })}countBreeds ()
A GET response can contain parameters in the URL, like this: https://site.com/?foo=bar
.
GET响应可以在URL中包含参数,例如: https://site.com/?foo=bar
: https://site.com/?foo=bar
。
With Axios you can perform this by using that URL:
使用Axios,您可以使用以下URL来执行此操作:
axios . get ( 'https://site.com/?foo=bar' )
or you can use a params
property in the options:
或者您可以在选项中使用params
属性:
axios . get ( 'https://site.com/' , { params : { foo : 'bar' }})
Performing a POST request is just like doing a GET request, but instead of axios.get
, you use axios.post
:
执行POST请求就像做一个GET请求,但不是axios.get
,您使用axios.post
:
axios . post ( 'https://site.com/' )
An object containing the POST parameters is the second argument:
包含POST参数的对象是第二个参数:
axios . post ( 'https://site.com/' , { foo : 'bar'})
翻译自:
axios 请求node
转载地址:http://jmqgb.baihongyu.com/