首页 > 代码库 > ajax:JSON

ajax:JSON

主页面:

<body>

<select id="nation">
    
</select>

<script type="text/javascript">

$.ajax({
        url:"jsonchuli.php",
        dataType:"JSON",
        success: function(data){
                var str ="";
                //方法一:循环
                /*for(var i=0;i<data.length;i++)
                {
                    str = str+"<option value=http://www.mamicode.com/‘"+data[i].Code+"‘>"+data[i].Name+"</option>";
                }*/
                //方法二:遍历
                for(var s in data)
                {
                    str = str+"<option value=http://www.mamicode.com/‘"+data[s].Code+"‘>"+data[s].Name+"</option>";
                }
                
                $("#nation").html(str);
            }
    });

</script>
</body>

php处理页面:

<?php
include("../DBDA.class.php");
$db = new DBDA();

$sql = "select * from nation";

//需要关联数组
//数组内容的编码格式:utf8的

echo json_encode($db->GuanQuery($sql));

ajax:JSON