示例

您所在的位置:网站首页 axios创建实例后可以调用什么 示例

示例

2024-06-05 15:08| 来源: 网络整理| 查看: 265

示例

使用 axios 的一个小示例

注意:CommonJS 用法

¥note: CommonJS usage

为了在 require() 中使用 CommonJS 导入时获得 TypeScript 类型(用于智能感知/自动补齐),请使用以下方法:

¥In order to gain the TypeScript typings (for intellisense / autocomplete) while using CommonJS imports with require() use the following approach:

const axios = require('axios').default; // axios. will now provide autocomplete and parameter typings 示例

¥Example

执行 GET 请求

¥Performing a GET request

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); }) .finally(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); }) .finally(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); } }

注意:async/await 是 ECMAScript 2017 的一部分,Internet Explorer 和旧版浏览器不支持,因此请谨慎使用。

¥NOTE: async/await is part of ECMAScript 2017 and is not supported in Internet Explorer and older browsers, so use with caution.

« 上一章 介绍 下一章 » POST 请求


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3