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;
}
本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢