编码我的 php 数组以显示在我的 javascript 代码中的问题

问题描述

我试图在我网站的 JavaScript 部分使用一个 PHP 数组,方法是将其编码为 Json 并通过回声发送,但编译网站并通过 chrome 访问它给我一个损坏的网站,其中包含一个停止的 Json 文件中途start of file end 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>

解决方法

检查 > $editor 的值,是否有任何特殊字符阻止进一步的 URL 文本 >>>

检查 URL/FileName 格式,将 Song-1.mp3 替换为 Song-1.mp3 用连字符替换空格/条纹/加号(在目录中)。

与加载 URL 结构相同,可能是单引号使代码混乱,例如:K'pop 歌曲