将从 JSON 响应返回的 HTML 片段插入到 DOM 中

发布于 2024-10-31 02:54:17 字数 902 浏览 0 评论 0原文

希望这不是一个愚蠢的问题,所以这里是:

我正在使用一个 Web 服务,它通过 jquery post 请求返回这样的 json:

{
"response": [
    [
        {
            "class": "class blah blah blah",
            "index": 0,
            "item": {
                "imageurl": "image.jpg",
                "description": "<span class=\"span1\"><span class=\"string1\">10 Great Gift Ideas for<\/span><span class=\"string2\">Valentines<\/span><span class=\"string3\">Day<\/br><\/span><span class=\"string4\">Only<br />5 days left<\/span><\/span>" 
            },
            "prices": null 
        } 
    ] 
]
}

这只是响应的一部分。我需要提取 response.item.description 并将其插入到页面上的 div 中,但是当我这样做时,额外的 \ 仍然存在,这会导致显示中断。有没有正确的方法来处理 html 片段?我在想我可以使用正则表达式来检查 \ 后跟一个 " / 等并将其删除。我也在想如果我将服务器端的数据更改为 HTML 编码,也许不会逃脱,但我宁愿保持数据原样。

jQuery 或 javascript 中是否有我缺少的内置内容?

Hopefully this isn't a stupid question so here goes:

I am using a Web Service that's returning json like this through a jquery post request:

{
"response": [
    [
        {
            "class": "class blah blah blah",
            "index": 0,
            "item": {
                "imageurl": "image.jpg",
                "description": "<span class=\"span1\"><span class=\"string1\">10 Great Gift Ideas for<\/span><span class=\"string2\">Valentines<\/span><span class=\"string3\">Day<\/br><\/span><span class=\"string4\">Only<br />5 days left<\/span><\/span>" 
            },
            "prices": null 
        } 
    ] 
]
}

That is just one part of the response. I need to extract the response.item.description and insert that into a div on the page but when I do, the extra \ are still there which causes the display to break. Is there a proper way to handle the html fragments? I was thinking that I could use a regex to check to see is a \ was followed by a " / etc and strip it. I was also thinking if I changed the data on the server side to be HTML encoded than maybe it wouldn't get escaped but I would rather keep the data the way it is.

Is there anything built into jQuery or javascript that I am missing?

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

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

发布评论

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

评论(1

稚然 2024-11-07 02:54:17

我认为你正在寻找 unescape()。你应该可以这样称呼它

unescape(response[0].item.description)

i think you're looking for unescape(). you should be able to call it like

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