ajax返回类型有TEXT,JSON,XML
XML:页面之间传递数据,跨平台传递
HTML:超文本标记语言,核心标签
XML特点:
1.标签名可以自己定义
2.有且只有一个根
3.大小写敏感
4.标签必须完整
例题方法一:
<body> <select id="sel"> </select> </body> <script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"ChuLi.php", dataType:"JSON", success: function(data){ //alert(data); var str = ""; for(var k in data) { str+="<option value=‘"+data[k][0]+"‘>"+data[k][1]+"</option>"; } $("#sel").html(str); } }); }); </script> </html>
处理页面php
<?php include("../DBDA.php"); $db = new DBDA(); $sql = "select * from Nation"; $attr = $db->Query($sql); echo json_encode($attr); ?>
XML的应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>无标题文档</title> <script src="../jquery-1.11.2.min.js"></script> </head> <body> <select id="sel"></select> </body> <script type="text/javascript"> $(document).ready(function(e) { $.ajax({ url:"chulixml.php", dataType:"XML", success:function(data){ var ch = $(data).find("nation").children(); var str = ""; for(var i=0;i<ch.length;i++) { var code = $(ch[i]).find("code").text(); var name = $(ch[i]).find("name").text(); str+="<option value=‘"+code+"‘>"+name+"</option>"; } $("#sel").html(str); } }); }); </script> </html>
例题方法二:
<?php include("../DBDA.php"); $db = new DBDA(); $sql = "select * from Nation"; $attr = $db->Query($sql); echo "<nation>"; foreach($attr as $k=>$v) { echo "<shuju{$k}>"; echo "<code>{$v[0]}</code>"; echo "<name>{$v[1]}</name>"; echo "</shuju{$k}>"; } echo "</nation>";
结果:
原文:http://www.cnblogs.com/xingfudehuanyan/p/5522850.html