axios get and post requests

The project uses axios, record the simple usage

1. Ordinary get request

axios.get('http://127.0.0.1:8080/test/delUser?userId='+id)
      .then((response) => {
        console.log(response.data);//return body of the request
      })
      .catch((error) => {
        console.log(error);//Exception
      });

Parameter passing can also be written like this

axios.get('http://127.0.0.1:8080/test/delUser',{
        params: {
          userId: id,
        }
      })
      .then((response) => {
        console.log(response.data);//return body of the request
      })
      .catch((error) => {
        console.log(error);//Exception
      });

2. Post request writing

axios.post('http://127.0.0.1:8080/test/login', {
            name: "admin",
            pwd: "123456"
          })
          .then(function (response) {
            console.log(response);
          })
          .catch(function (error) {
            console.log(error);
          });

这时候的post请求使用这这种请求,Spring MVC中直接@RequestParam 接收参数是接受不到的打开浏览器开发者工具会发现Request-Headers的Content-Typeapplication/json;charset=UTF-8如果不想使用application/json的解决方式:
          let param = new URLSearchParams();//Use URLSearchParams to pass parameters
          param.append("name", "admin");
          param.append("pwd", "123456");
          axios.post('http://127.0.0.1:8080/test/login',param)
          .then(function (response) {
            console.log(response);
          })
          .catch(function (error) {
            console.log(error);
          });

3. Concurrent multiple requests at one time

function getListOne(){
  return axios.get('http://127.0.0.1:8080/test/getListOne');
}
function getListTwo(){
  return axios.get('http://127.0.0.1:8080/test/getListOne');
}
axios.all([getListOne(),getListTwo()])
  .then(axios.spread(function(acct,perms){
    //Both requests successfully trigger this function, and the two parameters represent the results returned by the two requests
  }))

4.PS: axios does not support synchronous requests, you can use the method of operating after the request is successful instead of synchronous requests

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325771623&siteId=291194637