使用Zend
framework将JSON编码为
echo Zend_Json::enpre($jsonArray);
如果您已经使用JSON进行序列化,则不要以HTML标签发送图像。这样做的缺点基本上是Javascript代码除了将图像粘贴到页面中的某处之外不能对图像做太多事情。相反,只需将路径发送到JSON中的图像即可。
$jsonArray = array();$jsonArray['title'] = "Hello";$jsonArray['image'] = "<img src='images/bike.jpg' />";
在客户端,收到的JSON将如下所示:
{ "title": "Hello", "image": "<img src='images/bike.jpg' />"}因此,jQuery代码需要循环遍历每个键,然后使用匹配的键“ image1”或“ image2”将新图像注入div。
jQuery('.ajax').click(function(event) { event.preventDefault(); // load the href attribute of the link that was clicked jQuery.getJSON(this.href, function(snippets) { for(var id in snippets) { // updated to deal with any type of HTML jQuery('#' + id).html(snippets[id]); } });});


