【问题标题】:Problems with encoding my php array to show in my javascript code编码我的 php 数组以显示在我的 javascript 代码中的问题
【发布时间】: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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2013-04-01
      • 1970-01-01
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-05
      • 1970-01-01
      相关资源
      最近更新 更多