axios的使用

news/2024/10/18 14:21:44/

Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中。

如果您想在浏览器中使用 Axios,首先需要安装它。您可以使用 npm(Node 包管理器)或 yarn 来安装 Axios。例如,在命令行中输入以下命令:

shell复制代码

npm install axios

csharp复制代码

yarn add axios

安装完成后,您可以在 JavaScript 文件中引入 Axios,并使用它来发送 HTTP 请求。例如:

javascript复制代码

import axios from 'axios';
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
});

如果您想在 node.js 中使用 Axios,首先需要安装它。在命令行中输入以下命令:

shell复制代码

npm install axios

csharp复制代码

yarn add axios

安装完成后,您可以在 JavaScript 文件中引入 Axios,并使用它来发送 HTTP 请求。例如:

javascript复制代码

const axios = require('axios');
axios.get('/user?ID=12345')
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/qs/6.11.2/qs.js"></script>
</head>
<body><script>// axios({//     url:"",//     method:'',//     // 基础路径//     baseUrl:"",//     //get delete head 一类请求携带参数//     params:{//     },//     //post put patch 一类请求携带//     data:{//     },//     //请求头//     headers:{},//     //请求超过几秒中断请求//     timeout:2000// })// axios get 请求返回的是一个promise对象//    let res =  axios({//         url:"http://hmajax.itheima.net/api/ambition"//     }).then(res =>{//         console.log(res);//     }).catch(error=>{//         console.dir(error)//     })//     console.log(res);// get带参数 不需要转换数据格式 axios会自动转换为json查询字符串//  let res = axios({//     url:'http://121.199.0.35:8888/index/article/pageQuery',//     // get类请求携带参数选项 params只接受一个纯js对象 //     params:{//         page:1,//         pageSize:10//     }// })// console.log(res);// post请求 post 参数 json字符串或者表单格式数据// post发起请求默认数据格式为json格式 请求头Content-Type也会自动设置为application/json// let res =  axios({//     url:'http://121.199.0.35:8888/user/login',//     method:'post',//     data:{//         username:'admin1',//         password:123321//     }// })// console.log(res);//post 发起表单格式的请求let res = axios({url:"http://121.199.0.35:8888/baseUser/saveOrUpdate",method:'post',data:Qs.stringify({username:'aaa',password:1111}),headers:{'Authorization':'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJNalU9Iiwic3ViIjoiYWRtaW4xIiwiaXNzIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJpYXQiOjE3MDIyNjMyNTYsImF1ZCI6InJlc3RhcGl1c2VyIiwiZXhwIjoxNzAyNDM2MDU2LCJuYmYiOjE3MDIyNjMyNTZ9.JHmlslry8c_MfFCBH5Ld4PUxYU1-2nMWo2OhOXO3H3g'}})console.log(res);</script>
</body>
</html>

axios快捷方法

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title><script src="https://cdn.bootcdn.net/ajax/libs/qs/6.11.2/qs.js"></script><script src="https://cdn.bootcdn.net/ajax/libs/axios/1.5.0/axios.js"></script>
</head>
<body><script>// axios({})==>axios.request({})// 快捷方法 get无参  axios.get(url,请求配置项) // axios.get('http://121.199.0.35:8888/index/carousel/findAll').then(res=>{//   console.log(res.data,'获取响应');// });// get带参 // axios.get('http://121.199.0.35:8888/index/article/pageQuery',{//   params:{//     page:1,//     pageSize:10//   },//   headers:{//   },//   timeout:2000// }).then(res=>{//   console.log(res.data,'获取响应');// })// delete head options // axios.post(url,data,请求配置项) 发起post请求 数据格式会自动转为json格式// let data = {//   username:"admin1",//   password:123321// }// axios.post('http://121.199.0.35:8888/user/login',data,{//   timeout:2000,//   headers:{}// }).then(res=>{//   console.log(res);// })let data = {username:'测试用户9999888',password:654789,};axios.post('http://121.199.0.35:8888/baseUser/saveOrUpdate',Qs.stringify(data),{headers:{'Authorization': "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySWQiOiJNalU9Iiwic3ViIjoiYWRtaW4xIiwiaXNzIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJpYXQiOjE3MDIyNjMyNTYsImF1ZCI6InJlc3RhcGl1c2VyIiwiZXhwIjoxNzAyNDM2MDU2LCJuYmYiOjE3MDIyNjMyNTZ9.JHmlslry8c_MfFCBH5Ld4PUxYU1-2nMWo2OhOXO3H3g"}}).then(res=>{console.log(res.data,'获取响应');})</script>
</body>
</html>


http://www.ppmy.cn/news/1266205.html

相关文章

linux C++监听管道文件方式

方式一&#xff08;传统读取文件&#xff0c;一直监听循环读取文件&#xff09; 非阻塞打开文件&#xff0c;用read循环定时读取&#xff0c;性能不好 代码如下&#xff1a; #include <iostream> #include <fstream> #include <functional> #include <…

『CV学习笔记』轻量化卷积神经网络MobileNet从V1到V3

轻量化卷积神经网络MobileNet从V1到V3 文章目录 一. 预备知识1.1. 深度可分离卷积(Depthwise Separable Convolution,就是深度卷积+逐点卷积)1.1.1. 标准卷积(Standard convolutional)1.1.2. 深度卷积(Depthwise Convolution)1.1.3. 逐点卷积(Pointwise Convolution,就是11卷…

Docker笔记:Docker中简单配置Mysql/Redis/Mongodb容器

Docker 配置 Mysql 容器 1 &#xff09;方案1&#xff1a;基于centos等linux操作系统 启动centos镜像&#xff0c;在里面安装 mysql这样比较麻烦&#xff0c;配置的东西很多 … 2 &#xff09;方案2&#xff1a;直接用 mysql 镜像 (推荐) $ docker pull mysql 下载镜像$ do…

day45-46-Vue+ElementUI实现学生管理

VueElementUI实现学生管理 代码&#xff1a; qiushiju/java2313_vue_elementui_crud (gitee.com) 一、思考 考虑需求&#xff08;登录&#xff0c;查询全部&#xff0c;基本增删改查&#xff0c;分页&#xff0c;搜索&#xff0c;批量&#xff09; 设计数据库搭建项目 后端…

蓝牙配对、连接和删除汇总

目的&#xff1a;处理PC连接蓝牙设备的配对、连接和删除操作&#xff0c;以及常见故障。 命令行配对蓝牙设备并连接 要求&#xff1a;配对BLUET043蓝牙&#xff0c;密码为4444&#xff0c;然后连接该蓝牙。操作步骤如下&#xff1a; Step1.下载 修复工具&#xff0c;然后安装…

【后端学前端】第二天 css动画 动感菜单(css变量、过渡动画、过渡延迟、js动态切换菜单)

目录 1、学习信息 2、源码 3、变量 1.1 定义变量 1.2 使用变量 1.3 calc() 函数 4、定位absolute和fixed 5、transform 和 transition&#xff0c;动画 5.1 变形transform 5.2 transition 5.3 动画animation 6、todo 1、学习信息 视频地址&#xff1a;css动画 动感菜…

数据结构 | c++编程实现求二叉树的叶节点的个数。(递归非递归)

目录 非递归 递归 非递归 #include<iostream> #include<stack> using namespace std; struct BTNode {int data;BTNode* left, * right;BTNode(int val) :data(val), left(NULL), right(NULL) {}}; //递归的方式求二叉树的叶子结点数 int countnode(BTNode* t) …

JavaScript 函数的返回值

JavaScript 函数的返回值 JavaScript 函数的返回值是函数执行后返回的值&#xff0c;可以是任意类型的值&#xff0c;包括数字、字符串、布尔值、对象等。函数的返回值通过 return 关键字来指定&#xff0c;如果函数没有指定返回值&#xff0c;则默认返回 undefined。例如&…