如果使用查询参数作为第二个参数,Axios会传递标头不起作用

时间:2017-09-18 09:23:38

标签: javascript ecmascript-6 axios

我设法使用axios来执行带标题的GET请求

fetchUsers = ({offset = 1, q = ''} = {}) => new Promise(function (resolve, reject) {

  const url = `localhost:3000/users`

  axios.get(url, {
    headers: { token: 123 }
  }).then(response => {

  })
})

但我也想要分页和搜索的可选查询参数,我做

fetchUsers = ({offset = 1, q = ''} = {}) => new Promise(function (resolve, reject) {

  const url = `localhost:3000/users`

  axios.get(url, {params: {offset, q}}, {
    headers: { token: 123 }
  }).then(response => {

  })
})

我不再在标题中看到我的令牌,任何线索?

1 个答案:

答案 0 :(得分:0)

我认为,您应该尝试使用params将标题移动到对象:

  axios.get(url, {params: {offset, q}, 
                  headers: { token: 123 }}