Pass array via post in Vue.js

Asked

Viewed 397 times

0

I need to pass one array via POST, I tried it this way:

  const params = new URLSearchParams();
  params.append('pagamentos', this.variavel_array);
  axios.post('grava_pedido_pdv.php', params);

The variable variavel_array contains the value {'nome' : 'victor', 'cod' : 1}, however, on the console I visualize how "payments: [Object Object]" and I can’t read that figure in PHP.

1 answer

1


[object Object] is the return of the toString() method of an object. To send the object in JSON format you have to send JSON.stringify(variavel_array) but even better is to transform your object into a string of HTML parameters. Javascript does not have this native function, but you can create a helper:

function toParam(obj) {
  return Object.keys(obj).reduce(function(a,k){a.push(k+'='+encodeURIComponent(obj[k]));return a},[]).join('&')
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.