微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ExtJs4 笔记3 Ext.Ajax 对ajax的支持

本篇主要介绍一下ExtJs常用的几个对JS语法的扩展支持包括Ajax封装,函数事件操作封装,还有扩展的常用函数等。Ajax服务端交互式操作是提交到.NET MVC。后续服务端交互都采用这一方式实现。

一、ExtJs中的Ajax:Ext.Ajax.request

通过客户端向服务端发送Ajax请求,可以“直接”调用MVC的action方法,并传递参数,action返回值可以是普通字符串,也可以是json对象。请求可以添加自定义头信息。看下面例子:

1.异步请求,发送自定义请求头:

html页面如下:

[html]
?
@H_404_90@

我们先通过一个按钮单击事件触发ajax请求,服务端返回string到客户端,异步请求时,为请求添加自定义头“userHeader”,并在服务端获取。如下是js代码

[Js]
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@H_404_90@

服务端MVC接受请求的action代码

[C#]
5
@H_404_90@

然后我们在火狐中调试:自定义请求头已被添加

查看执行结果:

2.异步请求,返回json:

如果要返回json到客户端,MVC需要使用JsonResult的acton,自动将C#对象转换为json格式。客户端代码如下:

24
25
26
27
28
29
30
@H_404_90@

服务端代码

18
@H_404_90@
11
@H_404_90@
6
@H_404_90@

这样,每当ajax发起就可以被记录,可用作日志等。

二、元素对象Ajax式更新:Ext.Updater,[已弃用]

ExtJs为我们提供了异步方式更新dom元素内容支持。这可以用在一些异步交互性比较强的地方,例如webgame等。我们来看看具体实现方式。

我们先在视图中写这样的html:

"div1" title "d1" "width:50px; height:50px; position:absolute; background-color:Green; top:100px" >1</ >2</ >< "a" "20" />< "b" "50" /></ >
@H_404_90@

1.单个元素的及时更新

现在,我要通过c#在服务端计算a+b的值,然后将结果填到div1元素里面:

).getUpdater().update({
"Ext/UpdaterFunc" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
[c#]
ContentResult UpdaterFunc( Content( "a+b=" + (a + b));
}
@H_404_90@

页面加载后,将自动更新div1。再看看另外一种写法:

).load({
scripts: "请等待..."
2.通过提交表单更新
1
@H_404_90@
效果一样,只不过提交的参数来源于表单,参数名为对应的name值。

3.周期性更新

//周期性更新
up = Ext.Updater( );
up.startAutoRefresh(5,{ a: 10,b: 47 });
@H_404_90@
这段代码运行后将每隔5秒钟更新一次div1,用于需要适时核对服务端和客户端状态的情形。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐


1
2
3
4
< h1 >Ajax服务端请求</ >
div class = "content" id "div1" >
div >一共发起了< span "span1" style "color:Red" >0</ span >次请求。</ >
</ >
Ext.onReady( function () {
new Ext.Button({
renderTo: "div1" ,
text: "后台Ajax提交" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
handler: () {
Ext.Ajax.request({
url: 'Ajax_Func1' ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
headers: {
'userHeader' : 'userMsg'
},
params: { a: 10,b: 20 },
method: 'GET' ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
success: (response,options) {
Ext.MessageBox.alert( '成功' ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New', '从服务端获取结果: ' + response.responseText);
failure: ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',options) {
'失败' '请求超时或网络故障,错误编号:' + response.status);
}
});
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
id: "bt1"
});
});
public ContentResult Ajax_Func1( int a, b)
{
string userHeaderMsg = Convert.ToString(Request.Headers[ "userHeader" ]);
return Content((a + b).ToString() + ",userHeader:" + userHeaderMsg);
}
() {
Ext.Button({
"后台Ajax提交方式2返回JSON" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
() {
Ext.Ajax.request({
'Ajax_Func2' ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
params: { n: 10 },
'POST' callback: (options,success,response) {
if (success) {
var responseJson = Ext.JSON.decode(response.responseText);
Ext.Msg.alert( "成功" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',options.params.n + "的阶乘是:<font color='red'>" + responseJson.n1 + "</font><br />"
                      + options.params.n + "的累加是:<font color='red'>" + responseJson.n2 + "</font>" );
} else {
Ext.Msg.confirm( ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
错误编号:[' + response.status + ']是否要重新发送?' ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New', (btn) {
(btn == 'yes' ) {
Ext.Ajax.request(options);
}
});
}
}
});
}
});
});
JsonResult Ajax_Func2( n)
n1 = 1;
n2 = 0;
for ( i = 1; i <= n; i++)
{
n1 *= i;
n2 += i;
}
var data = new
{
n1 = n1,
n2 = n2
};
Json(data,JsonRequestBehavior.AllowGet);
查看执行结果,我们先看看火狐调试响应的结果,可以查看返回的json内容

界面执行结果:

3.异步文件上传

ExtJs通过强大的内部封装,使ajax请求看起来可以直接提交二进制流数据。每当提交文件数据时,ExtJs会自动创建iframe,在iframe中提交。提交完成后又自动移去,一切显得天衣无缝。下面演示一个通过异步请求无刷新文件上传的例子:

7
>Ajax文件上传</ "div2" form "form1" 请选择文本文件:< input type "file" name "file" />
"button" "button1" value "上传" />
form >
>
//文件上传
Ext.get( "button1" ).on( "click" () {
Ext.Ajax.request({
"Ajax_FileUp" isupload: true form: "form1" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',
(response) {
"上传成功,文本文件内容:" ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:2em!important; margin:0px!important; outline:0px!important; overflow:visible!important; padding:0px!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-family:'Courier New',response.responseText);
}
});
});
ContentResult Ajax_FileUp(HttpPostedFileBase file)
System.IO.StreamReader r = new System.IO.StreamReader(file.InputStream,System.Text.UTF8Encoding.Default);
var str = r.ReadToEnd();
Content(str);
提交后我们发现,上传的文本文件内容已经被正确读取。

4.异步请求事件

当发起ajax请求之前,可以监听beforerequest事件,本例每当发起ajax事件时,都会把计算器+1:

3
ajaxCount = 0;
//每当Ajax请求发起时触发:
Ext.Ajax.on( 'beforerequest' () { Ext.get( "span1" ).update(++ajaxCount) },255)!important">this );
).getUpdater().formUpdate( );