简单谈谈axios中的get,post方法
作者:jingxian 时间:2023-10-05 08:47:53
学习vue和nodejs的过程当中,涉及到了axios,今天为了测试,写了get和post两个方法来跟node服务端交互,结果因为header和参数弄了好久,在此记录一下,同时分享;
由于刚接触axios,在测试方法中,写的都是很简单的东西,不过能够实现基础功能,大神看到的话..非常欢迎指导..
//GET方法
axios.get(url, {
params: { 'key': 'value' }
}).then(function (response) {
alert(''.concat(response.data, '\r\n', response.status, '\r\n', response.statusText, '\r\n', response.headers, '\r\n', response.config));
}).catch(function (error) {
alert(error);
});
//对应服务端获取数据
const urlModule = require('url');
let params = urlModule.parse(request.url, true).query;//解析数据 获得Json对象
let value = params.key;//通过参数名称获得参数值
//POST方法
var params = new URLSearchParams();
params.append('key', 'value');
axios.post(url, params).then(function (response) {
alert(''.concat(response.data, '\r\n', response.status, '\r\n', response.statusText, '\r\n', response.headers, '\r\n', response.config));
}).catch(function (error) {
alert(error);
});
//对应服务端获取数据
const queryStringModule = require('querystring');
let postData = '';
request.on('data', function (chunk) {
postData += chunk;//接收数据
});
let params = queryStringModule.parse(postData);//解析数据 获得Json对象
let value = params.key;//通过参数名称获得参数值
此种写法猜测应该只是一种比较简单的实现,希望能够帮到其他人,同时希望高手指教。
标签:axios,get,post
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
W3C优质网页小贴士(四)
2008-04-17 13:34:00
js css 实现遮罩层覆盖其他页面元素附图
2024-06-08 21:51:06
![](https://img.aspxhome.com/file/2023/5/56475_0s.gif)
golang三元表达式的使用方法
2023-08-28 14:34:09
![](https://img.aspxhome.com/file/2023/5/87625_0s.jpg)
oracle 分页 很棒的sql语句
2009-07-02 11:44:00
Python控制Firefox方法总结
2023-09-08 04:00:17
![](https://img.aspxhome.com/file/2023/3/104893_0s.png)
javascript 选择文件夹对话框(web)
2024-04-17 09:48:43
![](https://img.aspxhome.com/file/2023/4/136534_0s.png)
python库-dotenv包 及 .env配置文件详解
2023-09-08 20:22:38
浅析pip安装第三方库及pycharm中导入第三方库的问题
2023-01-24 22:47:45
![](https://img.aspxhome.com/file/2023/2/65022_0s.png)
python实现TF-IDF算法解析
2021-06-02 03:27:51
![](https://img.aspxhome.com/file/2023/1/82961_0s.jpg)
基于python 凸包问题的解决
2021-04-11 02:56:41
![](https://img.aspxhome.com/file/2023/9/131359_0s.jpg)
python基础之递归函数
2021-11-29 03:28:30
![](https://img.aspxhome.com/file/2023/8/93538_0s.png)
python3.5 email实现发送邮件功能
2023-06-14 15:58:59
python让列表倒序输出的实例
2021-05-06 21:04:30
PWA介绍及快速上手搭建一个PWA应用的方法
2024-04-19 09:56:33
![](https://img.aspxhome.com/file/2023/1/136001_0s.jpg)
python实现弹跳小球
2022-05-30 08:55:08
![](https://img.aspxhome.com/file/2023/6/110446_0s.png)
Python 基于win32com客户端实现Excel操作的详细过程
2021-01-18 11:22:47
Python3 虚拟开发环境搭建过程(图文详解)
2021-11-19 00:34:51
![](https://img.aspxhome.com/file/2023/2/111872_0s.png)
python数据库编程 ODBC方式实现通讯录
2024-01-15 10:58:01
JS中的forEach、$.each、map方法推荐
2024-04-29 13:19:59
SQL函数substr使用简介
2024-01-27 11:12:02