javascript - 如何在需要 header 的axios发布请求中具有配置参数

标签 javascript reactjs xmlhttprequest httprequest axios

我试图在我的 react 代码中使用 axios 发送一个帖子请求,这两个都需要
“ header 参数”和“配置参数”同时进行。我发现在 axios 中写 post 请求有两种类型:

  • axios.post(url, 数据, 配置)
  • 轴({
    网址:网址,
    方法:'发布',
    标题:标题,
    数据:数据
    })

  • 在类型 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/

    相关文章:

    html - 在 html5 中播放 bytearray 中的音乐

    excel - xmlhttprequest 导致的 Unicode 字符

    javascript - 如何将父元素的高度设置为与子元素相同?

    javascript - 更改 iframe 内的 body bgcolor

    javascript - this.parentElement 是什么?

    reactjs - Draft.js - 从数据库中检索格式化文本

    javascript - React + Electron 如何打印 HTML?

    javascript - 我可以动态调用导入的文件名吗?

    javascript - React router 在每个路由组件中调用相同的代码

    javascript - 检测浏览器对跨域 XMLHttpRequests 的支持?