如何将json编码传递给PHP中的javascript?

时间:2019-06-24 08:00:16

标签: php json urlencode urldecode

我想对json进行编码,因为它包含特殊字符

我尝试使用简单的urlencode,但是它不起作用。 当我在js的警报中打印变量为空

$sql="select name,surname from agents";
$res = $conn->query($sql);
while($row = $res->fetch_assoc()) 
{
   $rows[] = $row;
}
$json = json_encode($rows);
echo urlencode($json);

2 个答案:

答案 0 :(得分:1)

如果要进行json编码,则需要将响应标头设置为application / json并转义unicode

header('Content-Type: application/json');
echo json_encode($rows, JSON_UNESCAPED_UNICODE); 

答案 1 :(得分:0)

如果要在javascript中显示json编码数组的内容,则首先需要decode

var array = JSON.parse(json_string);

接下来,您可以遍历数组并显示其内容。

相关问题