Home >Web Front-end >JS Tutorial >A brief introduction to the get and post methods in axios

A brief introduction to the get and post methods in axios

零下一度
零下一度Original
2017-06-27 15:23:251803browse

The following editor will bring you a brief talk about the get and post methods in axios. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

The process of learning vue and nodejs involves axios. For testing today, I wrote two methods, get and post, to interact with the node server. The result is because I have been working on header and parameters for a long time, so I will record them here and share them at the same time;

Since I am new to Axios, in the test method, I write very simple things, but they can achieve basic functions. If a master sees it... I welcome guidance...

//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;//通过参数名称获得参数值

This writing method is probably just a relatively simple implementation. I hope it can help others, and I hope experts can give me advice.

The above is the detailed content of A brief introduction to the get and post methods in axios. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn