JSON 传递字符串的问题,例如 // 或 #
Posted
技术标签:
【中文标题】JSON 传递字符串的问题,例如 // 或 #【英文标题】:Problem with JSON passing strings like // or # 【发布时间】: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 不起作用,或者我不确定,这就是我的想法。
【问题讨论】:
谢谢,我会编辑它 【参考方案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://***.com/a/37847337/362536)
通过使用请求正文,您不必担心长度限制,也不必担心 URI 编码。
另外,对于未来的 Stack Overflow 问题,请发布您的实际代码,而不是屏幕截图。
【讨论】:
以上是关于JSON 传递字符串的问题,例如 // 或 #的主要内容,如果未能解决你的问题,请参考以下文章
springMVC通过ajax传递参数list对象或传递数组对象到后台
Java:以 XML 或 JSON 格式请求正文而不使用字符串
iOS Swift 如何从 UIWebView 将 JSON 字符串和数组或字典传递给 Javascript?
将 JSON 字符串转换为 Java 对象或 HashMap