jquery ajax学习

http://hi.baidu.com/cxzhang/blog/item/0166563892cc65fbb211c7b0.html.

http://topic.youkuaiyun.com/t/20030527/22/1842509.html

 

using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using System.Reflection;


public class ToJson
{
    /// <summary>
    /// datatable to json
    /// </summary>
    /// <param name="dt"></param>
    /// <returns></returns>
    public static string DataTableToJson(DataTable dt)
    {      
        StringBuilder JsonString = new StringBuilder();
       if (dt != null && dt.Rows.Count > 0)
        {
         
            JsonString.Append("[ ");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                JsonString.Append("{ ");
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    if (j ==0)
                    {
                        JsonString.Append("/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/"");
                    }
                    else
                    {
                        JsonString.Append(",/"" + dt.Columns[j].ColumnName.ToString() + "/":" + "/"" + dt.Rows[i][j].ToString() + "/"");
                    }
                }
                /**/
                /*end Of String*/
                if (i == dt.Rows.Count - 1)
                {
                    JsonString.Append("} ");
                }
                else
                {
                    JsonString.Append("}, ");
                }
            }
            JsonString.Append("]");
            return JsonString.ToString();
        }
        else
        {
            return null;
        }
    }
}

 

 

前台

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"   
                    "http://www.w3.org/TR/html4/loose.dtd">  
<html>  
<head>  
<title></title>
  <script type="text/javascript" src="jquery-1.4.2.js"></script>  
    
  <script type="text/javascript"> 
  $(function(){  
    //点击按钮,并执行ajax请求  
    $("#ajaxReuqestID").click(function(){  
//            $.ajax({  
//                   url: "Handler.ashx",  
//                   beforeSend:function(){  
//                    $("#ajaxStateID").text("berforeSend");  
//                     alert("berforeSend");  
//                  },  
//                   success: function(html){  
//                    $("#ajaxStateID").text("success");
//     // $("#aaa").append(html); 
//                    alert(html);     
//                  },  
//                  error:function(){  
//                    $("#ajaxStateID").text("error");  
//                    alert("error");  
//                  },  
//                  complete:function(){  
//                    $("#ajaxStateID").text("complete");  
//                    alert("complete");  
//                  }  
//                 });  
//$.getScript("t.js", function(){
//  alert("Script loaded and executed.");
//});
$.getJSON(

    "Handler.ashx",
    function(json){
     $.each(json,function(i){
    // var s="asdfasdf";
    // alert(s);
   //  json=[{"name":"fan","age":26},{"name":"wang","age":25}];
    $("#cat-list").append("<li>name:"+json[i].name+"&nbsp; Age:"+json[i].age+" Age:"+json[i].pwd+"</li>")
    })
    });

//    var jsonStr = "{/"a/":/"内容1/", /"b/":/"内容2/"}";
//      $.each(jsonStr,function(i){
//    $("#aaa").append("<li>name:"+jsonStr[i].a+"  Age:"+jsonStr[i].b+"</li>")
//    })
//    });
//  var jsonObj = eval("(" + jsonStr + ")");
//  for(var property in jsonObj){
//   var nodeObj = document.getElementById(property);
//   if(nodeObj)
//    nodeObj.childNodes[0].nodeValue = jsonObj[property];
//  }

 

        });  
          
//        $("#ajaxStateID").ajaxStart(function(){  
//            $(this).text("ajaxStart");  
//            alert("ajaxStart");  
//        }).ajaxSend(function(){  
//            $(this).text("ajaxSend");  
//            alert("ajaxSend");  
//        }).ajaxSuccess(function(){  
//            $(this).text("ajaxSuccess");  
//            alert("ajaxSuccess");  
//        }).ajaxError(function(){  
//            $(this).text("ajaxError");  
//            alert("ajaxError");  
//        }).ajaxComplete(function(){  
//            $(this).text("ajaxComplete");  
//            alert("ajaxComplete");  
//        }).ajaxStop(function(){  
//            $(this).text("ajaxStop");  
//            alert("ajaxStop");  
//        });  
    })  
    
</script>  
   
</head>  
<body>  
    <input type="button" value="点击触发ajax请求" id="ajaxReuqestID"/>  
  <div id="ajaxStateID"></div>  
  <div id="aaa">
  </div>
  <ul id="cat-list"></ul>

</body>  
</html> 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值