zoukankan      html  css  js  c++  java
  • ajax 另外两种返回类型(json xml)

    返回json类型(例子):

    <!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="nation">
    	
    </select>
    
    <script type="text/javascript">
    
    $.ajax({
    		url:"jsonchuli.php",
    		dataType:"JSON",
    		success: function(data){
    				var s="";
    				/*for(var i=0;i<data.length;i++)
    				{
    					str = str+"<option value='"+data[i].Code+"'>"+data[i].Name+"</option>";
    				}*/
    				
    				for(var s in data)
    				{
    					str = str+"<option value='"+data[s].Code+"'>"+data[s].Name+"</option>";
    				}
    				
    				$("#nation").html(str);
    			}
    	});
    
    </script>
    </body>
    </html>
    

      

    <?php
    include("../DBDA.class.php");
    $db = new DBDA();
    
    $sql = "select * from nation";
    
    //需要关联数组
    //数组内容的编码格式:utf8的
    
    echo json_encode($db->GuanQuery($sql));
    

      返回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>
    
    <script type="text/javascript">
    $.ajax({
    		url:"xmlchuli.php",
    		dataType:"XML",
    		success: function(data){
    				//alert($(data).find("code").eq(1).text());
    				alert($(data).find("nation").attr("bs"));
    				
    				var allcode = $(data).find("code");
    				var allname = $(data).find("name");
    				var str ="";
    				for(var i=0;i<allcode.length;i++)
    				{
    					var code = allcode.eq(i).text();
    					var name = allname.eq(i).text();
    					
    					str = str +"<option value='"+code+"'>"+name+"</option>";
    				}
    				
    				$("#sel").html(str);
    			}
    	});
    </script>
    </body>
    </html>
    

      

    <?php
    include("DBDA.class.php");
    $db = new DBDA();
    
    $sql = "select * from nation";
    
    $attr = $db->Query($sql);
    
    echo "<nation bs='1'>"; foreach($attr as $k=>$v) { echo "<n{$k}><code>{$v[0]}</code><name>{$v[1]}</name></n{$k}>"; } echo "</nation>";

      

  • 相关阅读:
    Salesforce学习笔记(一)
    踏上Salesforce的学习之路(二)
    踏上Salesforce的学习之路(一)
    Salesforce注册开发者账号
    ubuntu下安装rtl8811cu/rtl8821cu网卡 Tplink WDN5200H网卡
    基于JRebel开发的MySQL Explain插件
    Logback配置解析
    基于springboot实现http响应异常信息国际化
    高并发场景下请求合并的实践
    后台开发常用mysql语句_v1.0
  • 原文地址:https://www.cnblogs.com/gaojunshan/p/6060615.html
Copyright © 2011-2022 走看看