Pass array via post in Vue.js

0

I need to pass an array via POST , I tried as follows:

  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, in the console I view it as " payload: " and I can not read this value in PHP .

    
asked by anonymous 17.10.2018 / 03:57

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 better still 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('&')
}
    
17.10.2018 / 04:03