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); ?>