ITPub博客

首页 > Linux操作系统 > Linux操作系统 > jQuery备忘之:jQuery处理.Net后台返回的Xml格式与Json格式的数据的比较分析

jQuery备忘之:jQuery处理.Net后台返回的Xml格式与Json格式的数据的比较分析

原创 Linux操作系统 作者:iDotNetSpace 时间:2010-05-20 17:26:16 0 删除 编辑
最近项目空闲下来,手头也没有什么事情,所以研究了一下jQuery对后台返回数据格式的处理。可能也是因为之前用JS写code的习惯,所以比较倾向于用JS处理XML格式的数据,当然这也就会造成我后台返回的数据的格式全部为XML的格式,处理起来相对繁琐。

      当然我也早就发现Jquery与Json格式的数据结合的非常好,但是一直苦于.NET的对数据转换成Json格式的繁琐,所以一直也没有采用Json格式的数据,或者是在数据相对简单的情况下采用Json格式的数据,其他千篇一律XML,但是今天我分析了一下Jquery对两种数据格式的处理之后,完全改变了心态,并力推大家用Jquery + Json处理后台返回数据。

      请看如下比较:

     【Jquery + Xml】

      如下代码是我查询IPInfo表中前10条数据,并以XML格式输出:

代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->public void XmlToAjax()
{
   DataTable dt 
= SQLHelper.ExecuteDataTable(SQLHelper.conStrings, CommandType.Text, "select top(10) * from IPInfo");
   DataSet ds 
= new DataSet();
   ds.Tables.Add(dt);
   
//Console
   Response.ContentType = "text/xml";
   Response.Charset 
= "UTF-8";
   Response.Clear();
   Response.Write(
""1.0\" encoding=\"utf-8\" ?>\n " + ds.GetXml());
   Response.End();
}

 

     我在前台接收到数据后进行如下处理:

代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->function checkText() {
            
var xmlT;
            
if ($.trim($("#txtID").val()) == "") {
                $(
"#sm").text("请输入XML条件");
                $(
"#txtID").focus();
                
return false;
            }
            
else {
                $(
"#sm").text("");
                $.ajax({
                    type: 
"GET",  
                  
//contentType: "application/json", //WebService 会返回Json类型
                    url: "../Ajax/Ajax1.aspx", 
                    dataType: 
"xml",
                    data: 
"",  //这里是要传递的参数,格式为 data: "{paraName:paraValue}",下面将会看到       
                    success: function(result) {     //回调函数,result,返回值
                        var ID = '';
                        $(result).find(
"Table1").each(function() {
                            ID 
+= $(this).children("IPid").text() + "
";
                        });
                        $(
"#divMsg").html(ID);
                    }
                });
            }

        }

 

     下面是输出结果:

     result.xml 的结果集为:

 

代码
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->result.xml
"
xml version="1.0"?>
<NewDataSet>
    
<Table1>
        
<IPid>1IPid>
        
<IPFrom>0.0.0.0        IPFrom>
        
<IPTo>0.255.255.255  IPTo>
        
<IPLocation>IANA  CZ88.NETIPLocation>
        
<IPToNumber>16777215IPToNumber>
        
<IPFromNumber>0IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>2IPid>
        
<IPFrom>1.0.0.0        IPFrom>
        
<IPTo>1.255.255.255  IPTo>
        
<IPLocation>IANA  CZ88.NETIPLocation>
        
<IPToNumber>33554431IPToNumber>
        
<IPFromNumber>16777216IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>3IPid>
        
<IPFrom>2.0.0.0        IPFrom>
        
<IPTo>2.255.255.255  IPTo>
        
<IPLocation>IANA  CZ88.NETIPLocation>
        
<IPToNumber>50331647IPToNumber>
        
<IPFromNumber>33554432IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>4IPid>
        
<IPFrom>3.0.0.0        IPFrom>
        
<IPTo>3.255.255.255  IPTo>
        
<IPLocation>美国 新泽西通用电气公司IPLocation>
        
<IPToNumber>67108863IPToNumber>
        
<IPFromNumber>50331648IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>5IPid>
        
<IPFrom>4.0.0.0        IPFrom>
        
<IPTo>4.9.255.255    IPTo>
        
<IPLocation>美国  CZ88.NETIPLocation>
        
<IPToNumber>67764223IPToNumber>
        
<IPFromNumber>67108864IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>6IPid>
        
<IPFrom>4.10.0.0       IPFrom>
        
<IPTo>4.10.255.255   IPTo>
        
<IPLocation>美国 新泽西州立大学IPLocation>
        
<IPToNumber>67829759IPToNumber>
        
<IPFromNumber>67764224IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>7IPid>
        
<IPFrom>4.11.0.0       IPFrom>
        
<IPTo>4.11.255.255   IPTo>
        
<IPLocation>美国 夏威夷IPLocation>
        
<IPToNumber>67895295IPToNumber>
        
<IPFromNumber>67829760IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>8IPid>
        
<IPFrom>4.12.0.0       IPFrom>
        
<IPTo>4.19.77.255    IPTo>
        
<IPLocation>美国  CZ88.NETIPLocation>
        
<IPToNumber>68374015IPToNumber>
        
<IPFromNumber>67895296IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>9IPid>
        
<IPFrom>4.19.78.0      IPFrom>
        
<IPTo>4.19.78.255    IPTo>
        
<IPLocation>美国 西南政法大学IPLocation>
        
<IPToNumber>68374271IPToNumber>
        
<IPFromNumber>68374016IPFromNumber>
    
Table1>
    
<Table1>
        
<IPid>10IPid>
        
<IPFrom>4.19.79.0      IPFrom>
        
<IPTo>4.19.79.63     IPTo>
        
<IPLocation>美国 Armed Forces Radio/TelevisionIPLocation>
        
<IPToNumber>68374335IPToNumber>
        
<IPFromNumber>68374272IPFromNumber>
    
Table1>
NewDataSet>
"

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/12639172/viewspace-663227/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-01-04

  • 博文量
    2376
  • 访问量
    5766305