关于前端:axios-axios中get请求的发送

6次阅读

共计 1308 个字符,预计需要花费 4 分钟才能阅读完成。

“Axios is a promise-based HTTP Client for node.js and the browser. It is isomorphic (= it can run in the browser and nodejs with the same codebase). On the server-side it uses the native node.js http module, while on the client (browser) it uses XMLHttpRequests.”

—https://axios-http.com/docs/intro

Axios 是一个基于 Promise 的 node.js 和浏览器的 HTTP 客户端。它是同构的(即能够在浏览器和 node.js 中应用雷同的代码库)。在服务器端,它应用原生的 node.js http 模块,而在客户端(浏览器)则应用 XMLHttpRequests。

对于发送 get 申请,axios 中能够有上面几种形式:

1、axios(config) (倡议应用)
举例:

axios({
  method: 'get',
  url: '/fund/info',
  params: {
    fundId: "000001",
    type: "bond"
  }
});

2、axios(url) || axios(url[, config]) (不倡议应用)
阐明:未指定办法,申请将默认为 GET 办法。
举例:

axios('/user/info');
axios('/management/doc', {
  responseType: "blob",
  timeout: 8000,
  params: {id: "abc123"}
});

3、axios.get(url) || axios.get(url[, config]) (倡议应用)
举例:

axios.get('/fund/basicInfo', {
  params: {fundId: "abc123"}
});

4、axios.request(config) (不倡议应用)
举例:

axios.request({
  method: 'get',
  url: "/management/doc"
  responseType: "blob",
  timeout: 8000,
      params: {id: "abc123"}
});

以上四种形式都是可行的,倡议在 1、3 中抉择一种,最好我的项目整体都选用同一种,放弃一致性。

下面各种形式的 params 最终都会变成 url 中的 query 参数

例如:

axios.get('/fund/info, {
  params: {
      fundId: "000001",
      type: "bond"
  }
});

最终发送的申请是 /fund/info?fundId=abc123&type=bond

在 params 中 key 存在,但 value 为 null 或 undefined 的参数不会在 URL 中显示。
例如:

axios.get('/fund/info, {
  params: {
      fundId: "000001",
      type: undefined
  }
});

最终发送的申请是 /fund/info?fundId=abc123

同步更新到本人的语雀
https://www.yuque.com/dirackeeko/blog/dnlygqwhft5qr1rm

正文完
 0