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. As a result, it took a long time to work on the header and parameters. I will record them here. , share at the same time;
Since I am new to axios, in the test method, I write very simple things, but it can realize the basic functions. If you see it, you are very welcome to give guidance. ..
##//GET method
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); });
//The corresponding server obtains data
const urlModule = require('url'); let params = urlModule.parse(request.url, true).query;//解析数据 获得Json对象 let value = params.key;//通过参数名称获得参数值
//POST method
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); });
//The corresponding server obtains data
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 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 Detailed explanation of get and post methods in axios. For more information, please follow other related articles on the PHP Chinese website!