实名认证用户熊川湘 身份证号码430811198506290914

jquery ajax的三种方法

jquery 返回值的的Ajax方法一
----------------------
js代码:
$(document).ready(function() {
  $('#tboxNum').change(function() {
      $('#err').html("tttt");
        var nownum=$('#tboxNum').val();
        $.get("default.aspx",{num:nownum},function(result) {
       
           var oldnum=parseInt(result);
          if (oldnum>nownum) {$('#err').html("不能大于"+oldnum+"数");}
          
       });
   });
 });
注意:url必须加一个时间戳防止缓存,否则的话它永远只取第一次的数据
$.get("/BBS_PROJECT_MY_SELF/showfriends.f?" + escape(new Date())

html页面代码
<input type="text" id="tboxNum" value="fff" />
<input id="dd" type="text" />
<div id="err">err</div>

aspx代码文件:
 if (Request.QueryString["num"] != null)
        {
            Response.Write("5");
            Response.End();
        }

jquery 返回值的的Ajax方法二
----------------------
js代码:
function CallMethod(method){
 $.ajax(
        {
          type: "POST",
          url: "default.aspx",
          data:{method:method},        
          success:function(msg){alert(msg);},
          error: function(){alert('出错了');}    
        }
      )
}

html页面代码:
<input type="button" value="调用Test" onclick="CallMethod('Test')"/><input type="button" value="调用Test1"

onclick="CallMethod('Test1')"/>

aspx代码文件:
  protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Form["method"] == "Test") Test();
        else if (Request.Form["method"] == "Test1") Test1();

      

    }

jquery 返回数据集的的Ajax方法二(通过json)
----------------------
js代码:
function CallDateMethod(method){
 $.ajax(
        {
          type: "POST",
          dataType:"json",
          url: "default.aspx",
          data:{method:method},        
          success:function(msg){
          var datename=msg.mytable;
          $.each(datename,function(n,i) {
          //$('#err').html($('#err').html()+"--"i.userid+"--"+i.username);
          $('#err').text($('#err').text()+"--"+i.userid+"--"+i.username);
          });
         
          },
          error: function(){alert('出错了');}    
        }
      )
}
html页面:
<input type="button" value="调用Test2" onclick="CallDateMethod('Test2')"/>

aspx代码文件:
 protected void Page_Load(object sender, EventArgs e)
    {

        if (Request.Form["method"] == "Test2") Test2();
    }

 public void Test2()
    {
        Response.Write(DataTable2Json(getdate()));
        Response.End();//停止其他输出
    }
  private string DataTable2Json(DataTable dt)
{
     StringBuilder jsonBuilder = new StringBuilder();
     dt.TableName = "mytable";
    jsonBuilder.Append("{\"");
     jsonBuilder.Append(dt.TableName);
    jsonBuilder.Append("\":[");
    for (int i = 0; i < dt.Rows.Count; i++)
    {
     jsonBuilder.Append("{");
     for (int j = 0; j < dt.Columns.Count; j++)
    {
    jsonBuilder.Append("\"");
    jsonBuilder.Append(dt.Columns[j].ColumnName);
     jsonBuilder.Append("\":\"");
    jsonBuilder.Append(dt.Rows[i][j].ToString());
     jsonBuilder.Append("\",");
     }
     jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
     jsonBuilder.Append("},");
     }
    jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
     jsonBuilder.Append("]");
     jsonBuilder.Append("}");
     return jsonBuilder.ToString();
  }

    protected DataTable getdate()
    {
        System.Data.DataTable dt = new System.Data.DataTable();
        SqlConnection con = new SqlConnection("Data Source=192.168.1.133;Initial Catalog=ladys2;Persist Security Info=True;User ID=sa;Password=");
        con.Open();
        SqlCommand cmd = new SqlCommand("select userid,username from users where userid<10", con);
       
           
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            con.Close();
       
        return dt;
    }

posted @ 2010-03-19 15:03  浪达短信群发  阅读(243)  评论(0编辑  收藏  举报