Home > Web Front-end > JS Tutorial > body text

Commonly used request method aliases based on Axios (detailed explanation)

亚连
Release: 2018-05-30 15:38:22
Original
1621 people have browsed it

Below I will share with you an article about Axios commonly used request method aliases, which has good reference value and I hope it will be helpful to everyone.

Axios

is a promise-based HTTP library that can be used in browsers and node.js.

Commonly used request method aliases generally include: Get/post/http protocol request

Execute Get request

function get(){
 return axios.get('/data.json', {
    params:{
     id:1234
    }
    }).then(function (response) {
     console.log(response);
    })
   .catch(function (error) {
    console.log(error);
   });
 }
Copy after login

When using the get method to pass parameters, the params method is used

Perform Post request

function post(){
return axios.post('/data.json', {
  id:1234
    })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
 }
Copy after login

When using the post method to pass parameters, the data is transferred directly. This is also the difference between the two methods.

Execute http protocol request

function http(){
 return axios({
 method: 'post',
 url: '/data.json',
 data: {
  id: 1111,
 },
params: {
 id:2222,
 }).then(res=>{
  this.msg=res.data;
 });
}
Copy after login

Pay attention to the difference here. When using post request, perform data processing The data method is used to pass parameters, and the params method is used when using get requests.

Use interceptors:

Intercept requests or responses before they are processed by then or catch.

// 添加请求拦截器
mounted:function(){
  axios.interceptors.request.use(function (config) {
    // 在发送请求之前做些什么
    return config;
   }, function (error) {
    // 对请求错误做些什么
    return Promise.reject(error);
   });
// 添加响应拦截器
  axios.interceptors.response.use(function (response) {
    // 对响应数据做点什么
    return response;
   }, function (error) {
    // 对响应错误做点什么
    return Promise.reject(error);
   });
}
Copy after login

The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.

Related articles:

jquery implements drag file upload loading progress bar function

Analysis of javascript prototype and prototype chain

Detailed tutorial on using Angular CLI to generate Angular 5 projects

The above is the detailed content of Commonly used request method aliases based on Axios (detailed explanation). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!