我使用javascript和php,需要在JSON变量中传递一些HTML (php ->JS)。
不幸的是,由于一些环境限制,我只能使用有限的jQuery。使用诸如jQuery.parseJSON(str)
和$.parseJSON(str)
之类的东西会抛出意外的令牌错误。
因此,我需要一个纯javascript的方法来处理JSON变量中的html。目前,HTML字符串只是作为字符串打印在页面上,尽管我需要它作为HTML生效。
我的JS代码如下:document.getElementById("activeDescription").innerHTML = response['description'];
,结果最终只是HTML页面上的文本,如下所示:
<p>helloworld</p>
而我期望的是
helloword
显示在HTML页面上。在警报(响应['description'])我收到
<p><span class="
编辑
当我使用
jQuery.parseJSON('{"name":"John"}');
一切都很好,但这段代码
jQuery.parseJSON(response['description']);
给了我一个"Uncaught SyntaxError: Unexpected token &"误差
很可能是编码问题。您可以通过解码字符来修复。
在javascript:var div = document.createElement('div');
div.innerHTML = encoded;
var decoded = div.firstChild.nodeValue;
在PHP中,查看以下链接:http://www.php.net/manual/en/function.htmlentities.php