Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Axios.Get with params and config together

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

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
})
like image 960
KitKit Avatar asked Jan 15 '18 10:01

KitKit


People also ask

How to send Axios GET request with headers and params?

You can use the params config option to set query string params. To send Axios GET request with Headers, we pass an option object with headers property. We can merge params and headers in a Axios GET request like this. You can perform an Axios POST object request with body as second parameter.

How do I add a parameter to an Axios query?

Axios GET with params You can use the params config option to set query string params. axios.get ('/bezkoder.com/tutorials', { params: { title: 'ios' } }); And this is equivalent:

How to pass a Param as a third argument in Axios?

After download the package using npm or yarn and import it, we can pass it as a third argument in the axios request as it follows: Note that when we map the params, we must return undefined if there is no value. The values null and '' (empty string) return the key with empty value by default. Otherwise, undefined omit the whole param.

How to add Axios to your project/code?

We can add Axios to our project/code with one of following simple ways: Axios Response Object has data field that contains the parsed response body. We use catch () for handling errors.


2 Answers

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:

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)
like image 57
casraf Avatar answered Sep 30 '22 18:09

casraf


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

like image 32
Biswajit Patra Avatar answered Sep 30 '22 16:09

Biswajit Patra