【问题标题】:Problem with JSON passing strings like // or #JSON 传递字符串的问题,例如 // 或 #
【发布时间】:2020-07-12 16:29:23
【问题描述】:

我正在尝试发送一个 axios 请求,其中包含来自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");

我有这个 PHP 文件:

$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);

问题是当我收到带有 // 或 # 的字符串时 JSON 不起作用,或者我不确定,这就是我的想法。

【问题讨论】:

  • 谢谢,我会编辑它

标签: php json vue.js axios


【解决方案1】:

在您的代码中,您有:

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

不要将数据连接到 URL。您正在使用 POST,因此您应该发布请求正文。

我不使用 Axios,所以我不能告诉你如何在那里使用它,但是对于大多数类似的实用程序,这是这样的:

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

在PHP端,可以通过解析结果来接收。 file_get_contents('php://input')(另见:https://stackoverflow.com/a/37847337/362536

通过使用请求正文,您不必担心长度限制,也不必担心 URI 编码。

另外,对于未来的 Stack Overflow 问题,请发布您的实际代码,而不是屏幕截图。

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 2011-07-11
    • 2015-04-18
    • 1970-01-01
    • 2019-01-31
    • 1970-01-01
    • 2011-07-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多