【问题标题】:Use Axios.Get with params and config together将 Axios.Get 与参数和配置一起使用
【发布时间】:2018-06-23 23:31:06
【问题描述】:

如何将axios.get 与参数和配置一起使用?我的代码不起作用。请帮我解决这个基本问题!

let config = {
    'headers': {'Authorization': 'JWT ' + this.$store.state.token}
}
let f = 0

axios.get('http://localhost:8000/api/v1/f/', {
    params: {
        page: f + 1
    },
    config: this.config
})

【问题讨论】:

    标签: javascript axios


    【解决方案1】:

    Axios 在第二个参数中采用整个配置,而不是配置对象列表。将参数放在配置中,并将整个对象作为第二个参数传递:

    let f = 0
    
    let config = {
      headers: {'Authorization': 'JWT ' + this.$store.state.token},
      params: {
        page: f + 1
      },
    }
    
    axios.get('http://localhost:8000/api/v1/f/', config)

    【讨论】:

      【解决方案2】:

      如果你想传递一个自定义标题,你可以这样做

      var config = {
          'Authorization': 'JWT ' + this.$store.state.token
      }
      let f = 0
      
      axios.get('http://localhost:8000/api/v1/f/', {
          params: {
              page: f + 1
          },
          headers: config
      })
      

      请按照文档查看更多要求,或者您也可以在此处发表评论。

      也试试这个:

      var config = {
          headers: {'Authorization': 'JWT ' + this.$store.state.token}
      };
      
      axios.get('http://localhost:8000/api/v1/f/',{
          params: {
              page: f + 1
          }}, config);
      

      文档:

      1. http://codeheaven.io/how-to-use-axios-as-your-http-client/

      2. I can't do a request that needs to set a header with axios

      3. https://github.com/axios/axios

      【讨论】:

      • headers 和 params 应该是配置对象中的兄弟姐妹。这个:axios.get('localhost', { params: {page: 1}, headers: config }) 不是这个:axios.get('localhost', { params: {page: 1}},config );
      • 如@getsetbro 所述,参数和标题应该是SIBLINGS。谢谢getsetbro
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-20
      • 1970-01-01
      • 2015-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多