如果使用查询参数作为第二个参数,Axios 将传递 header 无效
Axios get passing header not working if use query param as second argument
我设法使用 axios 通过 header
执行 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 => {
})
})
我在 header 中再也看不到我的令牌了,有什么线索吗?
我认为,您应该尝试使用参数将 headers 移动到 object:
axios.get(url, {params: {offset, q},
headers: { token: 123 }}
我设法使用 axios 通过 header
执行 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 => {
})
})
我在 header 中再也看不到我的令牌了,有什么线索吗?
我认为,您应该尝试使用参数将 headers 移动到 object:
axios.get(url, {params: {offset, q},
headers: { token: 123 }}