博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
axios 请求node_使用Axios的Node中的HTTP请求
阅读量:2508 次
发布时间:2019-05-11

本文共 4326 字,大约阅读时间需要 14 分钟。

axios 请求node

介绍 (Introduction)

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具有很多优势:

  • supports older browsers (Fetch needs a polyfill)

    支持较旧的浏览器(获取需要使用polyfill)
  • has a way to abort a request

    有办法中止请求
  • has a way to set a response timeout

    有办法设置响应超时
  • has built-in CSRF protection

    内置CSRF保护
  • supports upload progress

    支持上传进度
  • performs automatic JSON data transformation

    执行自动JSON数据转换
  • works in Node.js

    在Node.js中工作

视频教程 (A video tutorial)

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请求以发布数据:

安装 (Installation)

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" >

Axios API (The Axios API)

You can start an HTTP request from the axios object:

您可以从axios对象启动HTTP请求:

I use foo and bar as random names. Enter any kind of name to replace them.

我使用foobar作为随机名称 。 输入任何名称以替换它们。

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()

GET请求 (GET requests)

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 ()

向GET请求添加参数 (Add parameters to GET requests)

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'  }})

POST请求 (POST Requests)

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/

你可能感兴趣的文章
性能瓶颈定位整体思路
查看>>
十道海量数据处理面试题与十个方法大总结
查看>>
Archlinux GNOME 3 美化
查看>>
20189320《网络攻防》第五周作业
查看>>
2019.1.18笔记
查看>>
删除了Ubuntu之后,不能正常进入到Win7系统之中的解决办法
查看>>
写一个正则表达式匹配手机号
查看>>
Linux试题
查看>>
TableLock插件
查看>>
java 获取页面中的 a 标签 的 href 实例
查看>>
Knowledge Point 20180305 详解精度问题
查看>>
开发 Windows 8 Bing地图应用(4)
查看>>
mysql-python安装时mysql_config not found
查看>>
loadrunner 场景设计-添加Unix、Linux Resources计数器
查看>>
Python 基于python编写一些算法程序等
查看>>
Python 一键commit文件、目录到SVN服务器
查看>>
毕业5年决定你的命运 ----值得所有不甘平庸的人看看
查看>>
基于Python的接口自动化-01
查看>>
前台返回json数据的常用方式+常用的AJAX请求后台数据方式
查看>>
spring boot下MultipartHttpServletRequest如何提高上传文件大小的默认值
查看>>