此问题已在此处有答案:
Can I interpolate the result of json_encode directly into JavaScript code, or do I have to use JSON.parse?(2个答案)
23小时前关闭
$testArray = array("John", "Mary", "Peter", "Sally");
$myTest = json_encode($testArray);
$javascript = "
<script>
const displayExternalUrl = JSON.parse($myTest);
console.log(displayExternalUrl);
</script>
";
echo $javascript;
我尝试将$myTest(PHP)传递给constdisplayExternalUrl(Javascript),日志显示caught SyntaxError: Unexpected token 'J', "John,Mary,"... is not valid JSON at JSON.parse (<anonymous>)
错误代码在哪里?
1条答案
按热度按时间dwbf0jvd1#
设置
使用反引号中的$myTest变量
或引用