HTML 代码附加到 JSON 输出可能是由于 Google Docs Chrome 扩展造成的?

发布于 2024-12-26 04:12:55 字数 1039 浏览 1 评论 0原文

在与我的计算机不同的计算机上测试页面时,我注意到某些 Ajax 请求不起作用。查看控制台,我发现 JSON 字符串的末尾有一些 HTML 代码。这似乎与 Google 文档有关。我查了一下,似乎这个代码被附加到每个网页上。但是,如何才能阻止将此代码添加到 JSON 输出中呢?当 jQuery 尝试将其解释为 JSON 时,它显然会引发语法错误。

这段代码被附加到 JSON 输出中:

<div class="ugdv_contextMenu" id="ugdv_myMenu" style="display: none; ">
    <ul id="ugdv_contextMenu">
        <li id="ugdv_menuItem_google_docs">Open in Google Docs Viewer</li>
        <li id="ugdv_menuItem_new_tab">Open link in new tab</li>
        <li id="ugdv_menuItem_new_window">Open link in new window</li>
        <li id="ugdv_menuItem_new_incognito">Open link in new incognito window</li>
        <li class="ugdv_seperator"></li><li id="ugdv_menuItem_download_file">Download file</li>
        <li id="ugdv_menuItem_copy">Copy link address</li>
        <li id="ugdv_menuItem_editpdf">Edit PDF File on PDFescape.com</li>
    </ul>
</div>

那么,有人知道它是什么以及如何摆脱它吗?

谢谢!

Testing a page on a different computer to mine, I noticed that some Ajax request wasn't working. Looking in the console, I found that there was some HTML code at the end of the JSON string. It appears to be something to do with Google Docs. I checked, and this code is appended to every web page, it seems. How can I stop this code being added to JSON output, though? It obviously throws a syntax error as jQuery tries to interpret it as JSON.

This blob of code is appended to JSON output:

<div class="ugdv_contextMenu" id="ugdv_myMenu" style="display: none; ">
    <ul id="ugdv_contextMenu">
        <li id="ugdv_menuItem_google_docs">Open in Google Docs Viewer</li>
        <li id="ugdv_menuItem_new_tab">Open link in new tab</li>
        <li id="ugdv_menuItem_new_window">Open link in new window</li>
        <li id="ugdv_menuItem_new_incognito">Open link in new incognito window</li>
        <li class="ugdv_seperator"></li><li id="ugdv_menuItem_download_file">Download file</li>
        <li id="ugdv_menuItem_copy">Copy link address</li>
        <li id="ugdv_menuItem_editpdf">Edit PDF File on PDFescape.com</li>
    </ul>
</div>

So, does anyone know what it is and how I can get rid of it?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲念泪 2025-01-02 04:12:55

虽然我不知道为什么要附加此 HTML,但解决方案应该很简单:不要在 ajax 请求中指定 JSON 作为数据类型。事实上,根本不指定任何类型。只需在成功回调中执行以下操作:

success: function(response){
    var responseArray = response.split('<div');
    var jsonString = response[0];
    var obj = $.parseJSON(jsonString);
}

While I don't know why this HTML is getting appended, the solution should be simple enough: don't specify JSON as the data type in your ajax request. In fact, don't specify any type at all. Just do the following in your success callback:

success: function(response){
    var responseArray = response.split('<div');
    var jsonString = response[0];
    var obj = $.parseJSON(jsonString);
}
南烟 2025-01-02 04:12:55

该问题与 JSON 无关。如果您启用了 Google 文档支持,它似乎仅在 Chrome 中出现。 (这让我发疯,直到我意识到这是 Google 文档上下文菜单 - 好吧,我很慢。直到我启用文档后才显示;在其他浏览器上也不会显示。我花了很长时间进行更改我的ajax成功代码,直到我意识到它超出了我的控制范围。我看到的问题是,如果我尝试附加一个img或链接到一个div,上下文菜单的div会附加到我的文件名的末尾,导致。 html 错误。

例如 给定 $('

  • ').appendTo('#xxx').html(' ; 上下文菜单 div 紧随 .jpg 之后开始。

    The problem has nothing to do with JSON. It seems to appear only with Chrome IF you've enabled Google doc support. (This was driving me crazy until I realized it was the Google Docs context menu - ok, I'm slow. Didn't show up until I enabled docs; doesn't show up on other browsers. I spent an eternity making changes to my ajax success code until I realized it was beyond my control. The problem I'm seeing is that if I try to append an img or link to a div, the div for the context menu is appended to the end of my filename, causing html errors.

    E.g. given $('<li></li>').appendTo('#xxx').html('<img src="foo.com/bar.jpg" alt="" /> the context menu div starts right after the .jpg.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文