简体   繁体   中英

Passing params to POST call with axios

I'm trying to add the following query param into my axios call, but for some reason it's not accepting it.

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const params = {
  params: {
    isAccepted: true
  }
}

const url = `https://testapi.com`
axios.post(url, params, config)

However it works perfectly fine when I do it in this format

const config = {
  headers: {
    'Content-Type': 'application/json',
  }
}

const url = `https://testapi.com?isAccepted=true`
axios.post(url, params, config)

Am I using the params field wrong in the axios call?

'As can be seen in documentation, axios post request can accept params in config. You are trying to pass parameters on place where your post object should be (data). Parameters can be included in config though.

axios.post(url[, data[, config]])

Here is the documentation. https://github.com/axios/axios#axios-api

Try restructuring the code like this

const config = {
  headers: {
    'Content-Type': 'application/json',
  },
  params: {
     isAccepted: true
  }
}

const data = {
   data: 'whatever'
}

axios.post(url, data, config)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM