How to send asynchronous request using Axios?

First, let’s introduce Axios. Axios is a Promise-based HTTP client that helps us send asynchronous requests easily. It supports all modern browsers (including IE8+), and also provides a version for Node.js.

So, how to send an asynchronous request using Axios? Very simple! First, you need to install Axios. You can install it using npm or yarn:

npm install axios

Then, you can import Axios in your Vue component:

import axios from 'axios';

Now, you can use Axios to send asynchronous requests! Here is a simple example that sends a GET request to the server and prints the response data in the console:

axios.get('/api/data')  
  .then(response => {
    
      
    console.log(response.data);  
  })  
  .catch(error => {
    
      
    console.error(error);  
  });

In this example, we use Axios to send a GET request and print out the response data if the response is successful. If the request fails, we catch the error and print it to the console.

You can also send other types of requests, such as POST, PUT, DELETE, etc. Here is an example of a POST request:

axios.post('/api/data', {
    
     name: 'John Doe', email: '[email protected]' })  
  .then(response => {
    
      
    console.log(response.data);  
  })  
  .catch(error => {
    
      
    console.error(error);  
  });

In this example, we send a POST request to the server containing the name and email attributes. If the request is successful, we print out the response data. If the request fails, we catch the error and print it in the console.

You can also set request headers, configure request parameters, and more. Axios provides a number of useful options that help you tailor requests to your needs. You can check the Axios documentation for more information.

Hope these examples help you get started sending asynchronous requests with Axios! If you have any questions or need more help, please feel free to ask me.

Write a few more examples of Axios and how to package it into a practical utility function.

Send GET request

import axios from 'axios';  
  
function getData() {
    
      
  return axios.get('/api/data');  
}

In this example, we imported Axios, then defined a function called getData, which returns a GET request to Axios. You can call this function wherever you need to get data, for example:

getData().then(response => {
    
      
  console.log(response.data);  
}).catch(error => {
    
      
  console.error(error);  
});

Send POST request

import axios from 'axios';  
  
function postData(data) {
    
      
  return axios.post('/api/data', data);  
}

In this example, we define a function named postData, which receives a data object as a parameter and returns an Axios POST request. You can call this function wherever you need to send a POST request, for example:

const data = {
    
     name: 'John Doe', email: '[email protected]' };  
postData(data)  
  .then(response => {
    
      
    console.log(response.data);  
  })  
  .catch(error => {
    
      
    console.error(error);  
  });

In this example, we pass a data object containing name and email attributes to the postData function, and print out the response data after a successful response. If the request fails, we catch the error and print the error message in the console.
3. Encapsulate Axios into a tool function.
Now that we have two functions, we can encapsulate them into a tool function, as shown below:

import axios from 'axios';  
  
function request(method, url, data) {
    
      
  return axios({
    
      
    method,  
    url,  
    data,  
  });  
}

In this example, we imported Axios and then defined a utility function called request. This function receives three parameters: method, URL and data. It uses these parameters to create an Axios request and returns a Promise. You can call this function wherever you need to send a request, for example:

request('get', '/api/data')  
  .then(response => {
    
      
    console.log(response.data);  
  })  
  .catch(error => {
    
      
    console.error(error);  
  });

or

request('post', '/api/data', {
    
     name: 'John Doe', email: '[email protected]' })  
  .then(response => {
    
      
    console.log(response.data);  
  })  
  .catch(error => {
    
      
    console.error(error);  
  });

In this example, we use the request function to send GET and POST requests. After a successful response, we print the response data. If the request fails, we catch the error and print the error message in the console.

By encapsulating Axios, we can centralize the logic for sending requests in one place and easily create new utility functions to meet specific needs. This makes the code easier to maintain and extend, while also reducing redundant code.

Guess you like

Origin blog.csdn.net/2301_77795034/article/details/130935250