jquery php ajax json

just get a json

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <mce:script src="jquery-1.3.2.min.js" mce_src="jquery-1.3.2.min.js" type="text/javascript"></mce:script> </head> <body> <mce:script type="text/javascript"><!-- function getjson(){ $.getJSON('json.php',function(data){ var content=""; for(var i=0;i<data.length;i++){//循环读取后台的取得的JSON数据 content+=data[i].name; var par="排挡:"; for(var j=0;j<data[i].par.length;j++){//循环读取数据中的par集合 par+=data[i].par[j]+"、"; } content+=par+"<br/>"; } //alert(content); $('#jsondata').html(content); } ); } // --></mce:script> <a href="javascript:getjson();" mce_href="javascript:getjson();">getjson</a> <div id="jsondata" style="color:red" mce_style="color:red"></div> </body> </html>

 

post a json and get a json

 

<mce:script type="text/javascript" language="javascript"><!-- function del(com_id,meet_id){ $.ajax({ url:'json.php', type:'POST', //data:'id='+id, //dataType: 'html', data:{com_id:com_id,meet_id:meet_id}, dataType:"json", timeout: 1000, error: function(){ alert('Sorry, the server is busy now.'); }, success: function(data){ if(data){ // var content=""; for(var i=0;i<data.length;i++){//循环读取后台的取得的JSON数据 content+=data[i].name; var par="----"; for(var j=0;j<data[i].par.length;j++){//循环读取数据中的par集合 par+=data[i].par[j]+" , "; } content+=par+"/r/n"; } alert(content); //$('#jsondata').html(content); // }else{ alert("sorry, there is not any record here."); } } }); } // --></mce:script>

 

json.php

 

<?php //创建一个联合数组,这里我们尽量真实的模拟了一个从数据库中读取的记录 $a=array('name'=>'dog','sex'=>'f','par'=>array('li','wang','lian')); $b=array('name'=>'pig','sex'=>'m','par'=>array('liv','wange','liang')); $array = array(0=>$a,1=>$b); $json = json_encode($array); //将数组转化为一个json数据串 echo $json; /* [ {"name":"dog","sex":"f","par":["li","wang","lian"]}, {"name":"pig","sex":"m","par":["liv","wange","liang"]} ] Array ( [0] => Array ( [name] => dog [sex] => f [par] => Array ( [0] => li [1] => wang [2] => lian ) ) [1] => Array ( [name] => pig [sex] => m [par] => Array ( [0] => liv [1] => wange [2] => liang ) ) ) */ ?>

你可能感兴趣的:(jquery php ajax json)