【问题标题】:Script1014: Invalid Character replacing fetch with jQuery.ajax for IE 11 [duplicate]Script1014:IE 11 用 jQuery.ajax 替换 fetch 的无效字符 [重复]
【发布时间】:2020-05-01 10:53:26
【问题描述】:

我正在向旧的 javascript 组件添加 i18n 支持。它以前不需要 xhr/fetch,但它仍然需要在 IE11 中工作。

我不想写:

    fetch("/libs/cq/i8n/dict." + lang + ".json")
        .then(function(response) { return response.json(); })
        .then(function(data) {
            dictionary = data;
        });

但我知道 IE 不支持 fetch。我们有可用的 jQuery,所以我尝试了:

    jQuery.ajax({
        url: "/libs/cq/i18n/dict." + lang + ".json",
        success: function (response) {
            dictionary = JSON.parse(response);
            onReady();
        }
    })

现在我得到了这个:

SCRIPT1014:无效字符

index.js (49,17)

我意识到 49,17 指的是文件中的一个位置,第 49 行,字符 17。那是 dictionary = JSON.parse(response); 中的 d

我已经测试了响应,它是严格有效的 JSON。

由于某种原因,dictionary = eval(response) 正在工作。

【问题讨论】:

    标签: jquery internet-explorer


    【解决方案1】:

    问题是 jQuery 已经检测到它是 JSON 并为我解析它。

    Invalid Character 错误消息告诉我,我传递给 JSON.parse 的字符串是一个对象。

    我在第 49 行只需要:

                dictionary = response;
    

    【讨论】:

    • 奇怪这是问题所在,因为在任何浏览器中对对象调用 JSON.parse() 都是无效的;你会得到一个不同的错误。
    • 感谢您发布此问题的解决方案。我建议您在可以标记的 48 小时后尝试将自己的答案标记为该问题的已接受答案。它可以在未来帮助其他社区成员解决类似的问题。感谢您的理解。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多