HTML代码:
<script><br /> (function($){<br /> $.fn.RichmondJson = function(ajaxDiv)<br /> {<br /> $.ajax({<br /> url : 'data.php',<br /> type : 'get',<br /> dataType : 'json',<br /> data : {'inajax':1},<br /> timeout : 3000,<br /> cache : false,<br /> complete : function(){},<br /> success : function(data)<br /> {<br /> var s = '';<br /> $.each(data,function()<br /> {<br /> s += '<p>姓名:'+this.name+' 昵称:'+this.nick+'</script>
';<script><br /> function Richmond()<br /> {<br /> var rm = new Object;<br /> rm.loaded = false;<br /> rm.json = function()<br /> {<br /> if(!rm.loaded)<br /> {<br /> jQuery(this).RichmondJson.apply(this,arguments);<br /> rm.loaded = !rm.loaded;<br /> }<br /> else<br /> {<br /> alert('无需重复加载!');<br /> return false;<br /> }<br /> }</script>
return rm;
}
var myobj = new Richmond();
jQuery框架AJAX返回JSON数据
PHP代码:
if($_GET['inajax'])
{
$arr = array(
array('name' => 'Richmond', 'nick' => iconv('gbk','utf-8','财富星空')),
array('name' => '343548', 'nick' => iconv('gbk','utf-8','笨笨草')),
array('name' => 'iceblue', 'nick' => iconv('gbk','utf-8','冰蓝'))
);
$json_string = json_encode($arr);
echo $json_string;
//echo '
';
//$obj = json_decode($json_string,true);
//print_r($obj);
}
?>