发送请求 Post Requests

用Axios如何发起POST请求

发起一个 POST 请求

js
axios.post('/user', {
  firstName: 'Fred',
  lastName: 'Flintstone'
})
  .then((response) => {
    console.log(response)
  })
  .catch((error) => {
    console.log(error)
  })

发起多个并发请求

js
function getUserAccount() {
  return axios.get('/user/12345')
}

function getUserPermissions() {
  return axios.get('/user/12345/permissions')
}

const [acct, perm] = await Promise.all([getUserAccount(), getUserPermissions()])

// OR

Promise.all([getUserAccount(), getUserPermissions()])
  .then(([acct, perm]) => {
    // ...
  })

将 HTML Form 转换成 JSON 进行请求

js
const { data } = await axios.post('/user', document.querySelector('#my-form'), {
  headers: {
    'Content-Type': 'application/json'
  }
})

Forms

  • Multipart (multipart/form-data)
js
const { data } = await axios.post('https://httpbin.org/post', {
  firstName: 'Fred',
  lastName: 'Flintstone',
  orders: [1, 2, 3],
  photo: document.querySelector('#fileInput').files
}, {
  headers: {
    'Content-Type': 'multipart/form-data'
  }
})
  • URL encoded form (application/x-www-form-urlencoded)
js
const { data } = await axios.post('https://httpbin.org/post', {
  firstName: 'Fred',
  lastName: 'Flintstone',
  orders: [1, 2, 3]
}, {
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})