- <spanstyle="font-size:medium;">Ext.Ajax.request({
- url:'findbyid.action',
- params:{
- id:cell.getId()
- },250); line-height:18px"> success:function(resp,opts){
- varrespText=Ext.util.JSON.decode(resp.responseText);
- name=respText.name;
- oid=respText.id;
- findbyid(graph,cell,oid,name);
- //Ext.Msg.alert('错误',respText.name+"====="+respText.id);
- failure:function(resp,250); line-height:18px"> Ext.Msg.alert('错误',respText.error);
- }
- });</span>
在Ext开发过程中,基本上时刻需要用到异步请求,那么这个请求到底是怎么执行的呢,我们接下来来探讨下
首先:Ext.Ajax类继承了Ext.data.Connection,为Ajax的请求提供了最大灵活性的操作方式
说明的是这种请求通常都是放在触发某个事件的处理方法中的
url:就是我们要请求的路径
params:里面的参数用逗号隔开,就是我们要发出请求带的参数
success:是服务器处理成功返回
failure:是服务器处理失败返回
重点讲的就是如何处理返回值信息,我们的resp这个参数就显得非常重要了
resp是Ext构造的一个返回结果对象,如服务器端返回“this is a test!”(可以通过throw new Exception("this is a test!")简单实现)。那么返回将是
如下内容:
- <spanstyle="font-size:medium;">tId.1
- status.200
- statusText.OK
- getResponseHeader.[objectObject]
- getAllResponseHeaders.Server:Apache-Coyote/1.1
- Content-Type:text/html;charset=GBK
- Content-Language:zh-CN
- Content-Length:108
- Date:Wed,31Oct200712:51:23GMT
- responseText.
- <html>
- <head>
- <title>错误</title>
- </head>
- <body>
- <h1>错误:thisisatest!</h1>
- </body>
- </html>
- responseXML.
- argument.undefined</span>
从上面结果可以看出来,最开始是一些状态属性,我们也不常用,不管他。里面真正常用的是responseText与responseXML两个属性,那么这里面的responseText内容又被Ext用html包装了,但使用Ext.MessageBox展示出来正合适;reponseXML将在服务器端返回“text/xml”类型时使用。若服务器端返回是“text/json”类型时,客户端需要使用obj= Ext.util.JSON.decode(result.responseText);进行构造json对象,然后就可以正常使用了
具体操作返回值我们用JSON就这么写
ServletActionContext.getResponse().setContentType("text/json; charset=utf-8");
ServletActionContext.getResponse().getWriter().write("{success:true,info:'更新信息成功',name:'" + oo.getName()+ "',id:'" + id + "'}");
显然我这里返回的是JSON的值了(记住里面的属性值一定要加单引号)
var respText = Ext.util.JSON.decode(resp.responseText);
这个就可获得返回结果对象,要使用属性的话respText.id等都可直接用了
说到这里如果还想对这里面其他配置感兴趣的话可以参考下面的语句
该请求所用的http方面,默认值为配置的方法,或者当没有方法被配置时,如果没有发送参数时用get,有参数时用post。 The HTTP method to use for the request. Defaults to the configured method,or if no method was configured,"GET" if no parameters are being sent,and "POST" if parameters are being sent. Note that the method name is case-sensitive and should be all caps.
该方法被调用时附上返回的http response对象。不管成功还是失败,该回调函数都将被调用,该函数中传入了如下参数: The function to be called upon receipt of the HTTP response. The callback is called regardless of success or failure and is passed the following parameters:
- options: Object
- success: Boolean
请求成功则为true。True if the request succeeded.
- response: Object
包含了返回数据的xhr对象。The XMLHttpRequest object containing the response data. See http://www.w3.org/TR/XMLHttpRequest/ for details about accessing elements of the response.
为请求所加的请求头。 Request headers to set for the request.
原文地址:https://www.jb51.cc/ajax/166213.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。