0

I'm trying to send an axios request that contains a list of parameters, which are from JSON.stringify:

axios
      .post('../procesarProductos/2/'+JSON.stringify({
                                    id: this.id,
                                    cantidad: this.cantidad,
                                    nombre: this.nombre,
                                    precioventa: this.precioventa,
                                    precioproveedor: this.precioproveedor,
                                    imgurl: this.imgurl
                                    })+'/0/'+this.idafiliado) //Filtros
      .then(response => (this.productos = response.data))
      this.limipiar();
      swal("El producto ha sido actualizado", "", "success");

The PHP file i have this:

$elProducto = json_decode($params);
            $id=$elProducto->id;
            $nombre=$elProducto->nombre;
            $precioventa=$elProducto->precioventa;
            $precioproveedor=$elProducto->precioproveedor;
            $imgurl=$elProducto->imgurl;
            $cantidad=$elProducto->cantidad;
            $producto->modificarProducto($id,$idafiliado,$nombre,$precioventa,$precioproveedor,$imgurl,$cantidad);
            return $producto->listarProductos($idafiliado);

The problem is when i received the string with // or # JSON doesn't work, or well i'm not sure, that's what i think.

1 Answers1

0

In your code, you have:

.post('../procesarProductos/2/' + JSON.stringify({...})

Don't concatenate the data into the URL. You're using POST, so instead you should be posting a request body.

I don't use Axios so I can't tell you how to use it there, but with most similar utilities, this is something like:

.post('../procesarProductos/2', {
  body: JSON.stringify({...}),
  headers: {
    'Content-Type': 'application/json'
  }
)

On the PHP end, you can receive it by parsing the result of. file_get_contents('php://input') (See also: https://stackoverflow.com/a/37847337/362536)

By using the request body, you won't have to worry about length limits, nor URI encoding.

Also, please for future Stack Overflow questions, post your actual code and not screenshots.

Brad
  • 159,648
  • 54
  • 349
  • 530