【发布时间】:2021-04-14 23:27:57
【问题描述】:
我正在尝试在我的网站的 JavaScript 部分中使用 php 数组,方法是将其编码为 Json 并通过 echo 发送,但编译网站并通过 chrome 访问它会给我一个损坏的网站,其中包含一个停止的 Json 文件中途start of fileend of file
当我不对我的 Json 进行编码时,网站运行良好,而 Json 文件只有 265 kb 大,所以我猜网站在正常情况下应该能够处理。
如果有帮助,这里也是代码的相关部分:
<script type="javascript">
function confirmSelection(i) {
if (confirm("Sure you want to load new Song?")) {
let tags = {"'id'", "'number'", "'name'", "'slug'", "'way'",
"'language'", "'category'", "'year'", "'lyrics_author'",
"'melody_author'", "'position'", "'url'", "'update_time'", "'paragraphs'"};
let currentTags = {"'currentId'", "'currentNumber'", "'currentName'",
"'currentSlug'", "'currentWay'", "'currentLanguage'",
"'currentCategory'", "'currentYear'", "'currentLyrics_author'",
"'currentMelody_author'", "'currentPosition'", "'currentUrl'",
"'currentUpdate_time'", "'currentParagraphs'"};
<?php $json = json_encode($editor->lieder); ?>
let lieder = <?php echo $json; ?>;
for (let j = 0; j < tags.length; j++) {
document.getElementById(currentTags[j]).value = lieder[i][tags[j]];
}
}
}
</script>
【问题讨论】:
标签: javascript php arrays json encode