将从 JSON 响应返回的 HTML 片段插入到 DOM 中
希望这不是一个愚蠢的问题,所以这里是:
我正在使用一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你正在寻找 unescape()。你应该可以这样称呼它
i think you're looking for unescape(). you should be able to call it like