半个小时学json(json传递示例)
json传递(一维数组)
js代码
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $.getJSON("a.php",{id:1, name:encodeURI('李智'), gender:encodeURI('男')},function(json){ var json=eval(json); var html = '<select>' html += '<option value="' +json['id']+'">'+decodeURI(json['name'])+'</option>'; html += '</select>'; html += '<span>'+json['gender']+'</span>'; $('#aa').html(html); }); }); </script> <div id="aa"></div>
php代码
<?php /* write by lizhi 2009-10-26 */ $id = $_GET['id']; $name = $_GET['name']; $gender = $_GET['gender']; $arr2 = array("id"=>$id, "name"=>urldecode($name), "gender"=>urldecode($gender)); echo json_encode($arr2); ?>
上面的就是一个演示,具体的一般都是通过获取后台数据返回json
json传递示例(二维数组)
js代码
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $.getJSON("a.php",{id:1, name:'admin', gender:'男'},function(json){ var json=eval(json); var html = '<select>' $.each(json, function(k) { html += '<option value="' +json[k]['id']+'">'+decodeURI(json[k]['name'])+'</option>'; }); html += '</select>' $('#aa').html(html); }); }); </script> <div id="aa"></div> <!--利用jquery返回json数组,返回的为二维数组,所以需要遍历输出 未解决:中文传递问题-->
JQuery ajax 返回json时出现中文乱码该如何解决:http://www.jb51.net/article/72245.htm
php代码
<?php /* write by lizhi 2009-10-26 */ $json = array ( 0 => array ( 'id' => '13', 'name' => 'lizhi', ), 1 => array ( 'id' => '17', 'name' => 'admin', ) ); echo json_encode($json); ?>
赞 (0)