参考网址:https://www.npmjs.com/package/axios
const axios = require(‘axios‘);
// 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 });
// 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 });
// 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); } }
axios.post(‘/user‘, { firstName: ‘Fred‘, lastName: ‘Flintstone‘ }) .then(function (response) { console.log(response); }) .catch(function (error) { console.log(error); });
// Send a POST request axios({ method: ‘post‘, url: ‘/user/12345‘, data: { firstName: ‘Fred‘, lastName: ‘Flintstone‘ } });
可以同时请求多个接口
axios.all( [getUserAccount(), getUserPermissions()]) .then( axios.spread(function(acct,perms)) )
原文:https://www.cnblogs.com/pwindy/p/15161919.html