Home  >  Article  >  Web Front-end  >  Detailed explanation of get and post methods in axios

Detailed explanation of get and post methods in axios

PHP中文网
PHP中文网Original
2017-06-22 14:04:171876browse

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!

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