javascript 将 Axios.Get 与 params 和 config 一起使用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48261227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:55:06  来源:igfitidea点击:

Use Axios.Get with params and config together

javascriptaxios

提问by KitKit

How do I use axios.getwith params and config together? My code is not working. Please help me this basic issue!

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

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
})

回答by casraf

Axios takes the entire config in the second argument, not a list of config objects. Put the params inside the config, and pass the entire object as the second argument:

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)

回答by Biswajit Patra

If you want to pass a custom header you can do so by

如果您想传递自定义标头,您可以通过

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
})

Please follow the documentation to so see any further requirements, or you can comment here too.

请按照文档查看任何进一步的要求,或者您也可以在这里发表评论。

Try this out too:

也试试这个:

var config = {
    headers: {'Authorization': 'JWT ' + this.$store.state.token}
};

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

Documention:

文档:

  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

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

  2. 我无法执行需要使用 axios 设置标头的请求

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