Axios请求参数说明

783 阅读3分钟

Axios的请求方式别名如下

axios.request(config)
axios.get(url[, config]) 
axios.delete(url[, config]) 
axios.head(url[, config]) 
axios.options(url[, config]) 
axios.post(url[, data[, config]]) 
axios.put(url[, data[, config]]) 
axios.patch(url[, data[, config]])`
  1. axios.request(config): 发送自定义请求,其中 config 是一个对象,包含请求的配置信息,例如 URL、请求参数、请求头等。 参数类型要求: config 对象包含以下属性:
  • url: 请求的 URL 地址
  • method: 请求方法(GET、POST、PUT、PATCH 或 DELETE)
  • params: 请求的参数对象,可选参数
  • data: 请求体内容
  • headers: 请求头对象
  • timeout: 请求超时时间
  • withCredentials: 表示跨域请求时是否需要携带 cookie
  1. axios.get(url[, config]): 发送 GET 请求,其中 url 表示请求的 URL,config 可选参数表示请求的配置对象。 参数类型要求:第一个参数是字符串类型,表示请求的 URL。第二个参数是可选的请求配置对象,包括与 axios.request(config) 相同的配置项。

  2. axios.delete(url[, config]): 发送 DELETE 请求,参数与 axios.get(url[, config]) 相同。

  3. axios.head(url[, config]): 发送 HEAD 请求,参数与 axios.get(url[, config]) 相同。

  4. axios.options(url[, config]): 发送 OPTIONS 请求,参数与 axios.get(url[, config]) 相同。

  5. axios.post(url[, data[, config]]): 发送 POST 请求,其中 url 表示请求的 URL,data 表示请求体中的内容,config 表示请求的配置对象。 参数类型要求:第一个参数是字符串类型,表示请求的 URL。第二个参数是可选的请求体内容,可以是文本、JSON 对象、Blob、ArrayBuffer 等。第三个参数是可选的请求配置对象,包括与 axios.request(config) 相同的配置项。

  6. axios.put(url[, data[, config]]): 发送 PUT 请求,参数与 axios.post(url[, data[, config]]) 相同。

  7. axios.patch(url[, data[, config]]): 发送 PATCH 请求,参数与 axios.post(url[, data[, config]]) 相同。

使用这些 API 时,一般需要提供一个请求 URL,有时需要提供请求体内容和其他请求的配置信息。下面是一些示例:

  1. 发送 GET 请求:
axios.get('/user?ID=12345')
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
  1. 发送 POST 请求:
axios.post('/user', {
    firstName: 'John',
    lastName: 'Smith'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
  1. 发送 PUT 请求:
axios.put('/user/12345', {
    firstName: 'John',
    lastName: 'Doe'
  })
  .then(function (response) {
    console.log(response);
  })
  .catch(function (error) {
    console.log(error);
  });
  1. 发送 DELETE 请求:

axios.delete('/users/1', {
  headers: {
    Authorization: 'Bearer TOKEN'
  }
});

5.发送HEAD请求:

axios.head('/users', {
  params: {
    name: 'John'
  }
});

6.发送OPTIONS

axios.options('/users');

7.发送PATCH

axios.patch('/users/1', {
  firstName: 'John',
  lastName: 'Doe'
});

对axios的各种请求方式进行分类,按照使用查询字符串传参或占位符传参的方式分类如下:

使用查询字符串传参的请求方式:

  1. GET请求:使用查询字符串传递参数
axios.get('/api/user', {
  params: {
    userId: '123',
    userName: 'John Doe'
  }
})
.then(response => {
  console.log(response.data);
})
  1. DELETE请求:使用查询字符串传递参数
axios.delete('/api/user', {
  params: {
    userId: '123',
    userName: 'John Doe'
  }
})
.then(response => {
  console.log(response.data);
})

使用占位符传参的请求方式:

  1. GET请求:使用占位符传递参数
axios.get('/api/user/{userId}', {
  params: {
    userId: 123
  }
})
.then(response => {
  console.log(response.data);
})
  1. DELETE请求:使用占位符传递参数
axios.delete('/api/user/{userId}', {
  params: {
    userId: 123
  }
})
.then(response => {
  console.log(response.data);
})
  1. POST请求:使用占位符传递参数
axios.post('/api/user/{userId}', {
  name: 'John Doe',
  age: 30
})
.then(response => {
  console.log(response.data);
})
  1. PUT请求:使用占位符传递参数
axios.put('/api/user/{userId}', {
  name: 'John Doe',
  age: 30
})
.then(response => {
  console.log(response.data);
})

在这些例子中,使用查询字符串传参的请求方式在URL后面以?开始,参数以键值对的形式添加。而使用占位符传参时,URL中用花括号({})包裹参数名。无论是使用查询字符串传参还是占位符传参,axios都可以很方便地处理这些请求。根据具体的需求,选择适合的参数传递方式即可。