我试图在我的 react 代码中使用 axios 发送一个帖子请求,这两个都需要
“ header 参数”和“配置参数”同时进行。我发现在 axios 中写 post 请求有两种类型:
网址:网址,
方法:'发布',
标题:标题,
数据:数据
})
在类型 1 中我们不能发送 headers 参数,在类型 2 中我们不能发送配置参数。
那么有没有办法解决这个问题呢?
I solved it using xml httpRequest instead of axios, but I'm curious about the way we could solve it using axios.
最佳答案
基于doc
您可以设置标题 在 配置 !
axios.post(url, data, {headers : {'X-Requested-With': 'XMLHttpRequest'} })
或者您可以将所有选项作为对象发送
axios.request ({
url: '/user',
method: 'post',
data: {
firstName: 'Fred'
},
headers: {'X-Requested-With': 'XMLHttpRequest'},
// ... and other options
})
关于javascript - 如何在需要 header 的axios发布请求中具有配置参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52282683/