我想问,在我通过 ajax 请求访问的 php 脚本中,我返回 json 数据(从数组转换)
echo json_encode($row_array);
我在 jquery 中获取这些数据并将其显示在表单中。在返回数据之前我需要应用 htmlspecialchars/htmlentites 吗?
那么正确的做法是什么?以下代码给我一个错误:
echo htmlentities(json_encode($row_array));
谢谢你 伊姆兰
最佳答案
不要以这种方式应用 htmlentities。您应该在对数组进行 json 编码之前遍历该数组并转义每个元素,然后对可安全显示值的数组进行 json 编码。在您的使用中,json 只是数组的传输层。您不显示 json 数组,仅显示元素数据。不要逃避传输层 - 它可能会使 json 字符串无效。
关于php - 我需要在 json 数组上应用 htmlspecialchars/htmlentites 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12331933/