【发布时间】:2020-07-12 10:54:35
【问题描述】:
我想通过script-tag 提供我的 JSON 数据
<?php
$myArray = array(
'a' => 'Hello world!',
'b' => '</script>',
'c' => 123.456,
);
$myJson = json_encode($myArray);
?>
<script id="mydata" type="application/json"><?=$myJson?></script>
然后我想用 Javascript 取回我的 JSON 数据:
try {
var myArray = JSON.parse(document.getElementById('mydata').innerHTML);
console.log(myArray);
} catch(e) {
console.log(e.message);
}
如何在 PHP 中转义 $myJson-string 以避免 HTML 错误?
【问题讨论】:
标签: javascript php html json