zoukankan      html  css  js  c++  java
  • axios---get和post用法详解

    参考网址:https://www.npmjs.com/package/axios

    1.axios发送get请求

    const axios = require('axios');

    1.1.参数写在路径后面

    // Make a request for a user with a given ID
    axios.get('/user?ID=12345')
      .then(function (response) {
        // handle success
        console.log(response);
      })
      .catch(function (error) {
        // handle error
        console.log(error);
      })
      .then(function () {
        // always executed
      });

    1.2.参数不写在路径后面

    // Optionally the request above could also be done as
    axios.get('/user', {
        params: {
          ID: 12345
        }
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      })
      .then(function () {
        // always executed
      });  

    1.3.用异步async/await语法

    // Want to use async/await? Add the `async` keyword to your outer function/method.
    async function getUser() {
      try {
        const response = await axios.get('/user?ID=12345');
        console.log(response);
      } catch (error) {
        console.error(error);
      }
    }

    2.axios发送post请求

    2.1.常规用法

    axios.post('/user', {
        firstName: 'Fred',
        lastName: 'Flintstone'
      })
      .then(function (response) {
        console.log(response);
      })
      .catch(function (error) {
        console.log(error);
      });

    2.2.复合用法

    // Send a POST request
    axios({
      method: 'post',
      url: '/user/12345',
      data: {
        firstName: 'Fred',
        lastName: 'Flintstone'
      }
    });

    3.axios.all用法(Promise.all)

    可以同时请求多个接口

    axios.all( [getUserAccount(), getUserPermissions()])
        .then(  axios.spread(function(acct,perms))  )

    4.axios拦截器

    4.1.请求拦截---request

    // Add a request interceptor
    axios.interceptors.request.use(function (config) {
        // Do something before request is sent
        return config;
      }, function (error) {
        // Do something with request error
        return Promise.reject(error);
      });

    4.2.响应拦截---response

    // Add a response interceptor
    axios.interceptors.response.use(function (response) {
        // Any status code that lie within the range of 2xx cause this function to trigger
        // Do something with response data
        return response;
      }, function (error) {
        // Any status codes that falls outside the range of 2xx cause this function to trigger
        // Do something with response error
        return Promise.reject(error);
      });
  • 相关阅读:
    转-mysql中int、bigint、smallint 和 tinyint的区别详细介绍
    转-通用接口测试用例设计
    转-接口测试
    剑指offer 面试题43 n个骰子的点数 DP
    HYSBZ 1503 郁闷的出纳员 伸展树
    HYSBZ 1588 营业额统计 平衡二叉树模板
    剑指offer 面试题35 第一个只出现一次的字符
    LightOJ 1030 Discovering Gold 期望
    LightOJ 1027 A Dangerous Maze 概率期望
    剑指offer 面试题34 丑数
  • 原文地址:https://www.cnblogs.com/pwindy/p/15161919.html
Copyright © 2011-2022 走看看