Home > Net >  Passing multiple params issue in VUE.JS
Passing multiple params issue in VUE.JS

Time:12-02

I am trying to get data by passing multiple ids as param.But the params i am passing is going as array which i don't want.

Example:

Current data: The api is returning param as details?ids[]=123&ids[]=245

Expected data: I would like to pass the param as details?ids=123&ids=245

  const postRes = await axios.post('employee/departments', this.details);
  const getRes = await axios.get('employee/departments',
     { params: { ids: postRes.map(i=>i.id)},
  });
  this.$emit('fileDetails', getRes.data);

CodePudding user response:

You could serialize the parameters yourself:

  1. Map each object into a string, containing 'ids=' prefixed to the object's id.
  2. Join the resulting array with a & delimiter.
const params = postRes.map(i => 'ids='   i.id) 1️⃣
                      .join('&') 2️⃣
const getRes = await axios.get('employee/departments?'   params)

Run this snippet for example output:

const postRes = [{ id: 11 }, { id: 22 }, { id: 33 }]
const url = 'employee/departments?'   postRes.map(i => 'ids='   i.id).join('&')
console.log(url)
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related