
























很多人都为javascript而感到困惑,写个效果太复杂了,有了jquery后,我们就能够很简单的写一些很漂亮的效果。而这次我要说的不是这个,是jquery对ajax的支持,下面先简单说一下jquery对ajax的一些函数
通用方式:
$.ajax(prop) 通过一个ajax请求,回去远程数据,prop是一个hash表,它可以传递的key/value有以下几种。
(String)type:数据传递方式(get或post)。
((String)url:数据请求页面的url
((String)data:传递数据的参数字符串,只适合post方式
((String)dataType:期待数据返回的数据格式(例如 "xml", "html", "script",或 "json")
((Boolean)ifModified: 当最后一次请求的相应有变化是才成功返回,默认值是false
((Number)timeout:设置时间延迟请求的时间。可以参考$.ajaxTimeout
((Boolean)global:是否为当前请求触发ajax全局事件,默认为true
((Function)error:当请求失败时触发的函数。
((Function)success:当请求成功时触发函数
((Function)complete:当请求完成后出发函数
1
$.ajax({url: "ajax.htm",
2
success:function(msg){
3
$(div"#a").html(msg);
4
}
5
});
1
$.ajax({ url: "ajax.aspx",
2
type:"get",
3
dataType:"html",
4
data: "name=John&location=Boston",
5
success:function(msg){
6
$("#a").html(msg);
7
}
8
});
其它简化方式:
$.get(url, params, callback) 用get方式向远程页面传递参数,请求完成后处理函数,除了url外,其它参数任意选择!
$.get( "ajax.htm" , function(data){ $("#a").html(data) })
$.get( "ajax.asp",
{ name: "young", age: "25" },
function(data){ alert("Data Loaded: " + data); }
)
$("#a").load("ajax.htm", function() { alert("load is done"); } );
向ajax.htm页面发出请求,将返回结果装入id为a的内容中,然后再执行函数callback。 1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
<html xmlns="http://www.w3.org/1999/xhtml" >
3
<head>
4
<title>Untitled Page</title>
5
<script src="../jquery.js"></script>
6
<script language=javascript>
7
$(document).ready(
8
function()
9
{
10
$("#ajbt").click(
11
function()
12
{
13
$("#loadimg").ajaxStart(
14
function()
15
{
16
$("#loadimg").css("display","block");
17
}
18
);
19
20
// $.ajaxTimeout( 500000000 );
21
$.ajax(
22
{
23
type: "get",
24
url: "Default2.aspx",
25
datatype:"text",
26
data: "name=John",
27
complete:function()
28
{
29
$("#loadimg").css("display","none");
30
},
31
success: function(msg){
32
33
34
$("#a").html(msg);
35
}
36
}
37
);
38
}
39
);
40
$("#loadimg").css("display","none");
41
}
42
);
43
44
</script>
45
</head>
46
<body>
47
<input type=button value="ajaxdata" id="ajbt"/>
48
<div id="a"><img src=2.gif id="loadimg" ></div>
49
</body>
50
</html>
51
1
protected void Page_Load(object sender, EventArgs e)
2
{
3
System.Threading.Thread.Sleep(3000);
4
if (!this.IsPostBack)
5
{
6
if (Request["name"].ToString() != "")
7
{
8
httpresponse(Request["name"].ToString());
9
}
10
else
11
{
12
httpresponse(Request["name"].ToString());
13
}
14
}
15
}
16
public void httpresponse(string name)
17
{
18
//HttpContext.Current.Response;
19
HttpContext.Current.Response.Write(name);
20
}
21
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。