我是靠谱客的博主 耍酷大山,这篇文章主要介绍简单谈谈axios中的get,post方法,现在分享给大家,希望可以做个参考。

学习vue和nodejs的过程当中,涉及到了axios,今天为了测试,写了get和post两个方法来跟node服务端交互,结果因为header和参数弄了好久,在此记录一下,同时分享;

由于刚接触axios,在测试方法中,写的都是很简单的东西,不过能够实现基础功能,大神看到的话..非常欢迎指导..

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
//GET方法 axios.get(url, {   params: { 'key': 'value' } }).then(function (response) {   alert(''.concat(response.data, 'rn', response.status, 'rn', response.statusText, 'rn', response.headers, 'rn', 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, 'rn', response.status, 'rn', response.statusText, 'rn', response.headers, 'rn', 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方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

最后

以上就是耍酷大山最近收集整理的关于简单谈谈axios中的get,post方法的全部内容,更多相关简单谈谈axios中内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(130)

评论列表共有 0 条评论

立即
投稿
返回
顶部