当前位置: 首页 > 图文教程 > 网络编程 > ASP.NET > 官网 Ext direct包中.NET版的问题

ASP.NET
GridView添加删除按钮终极办法
AjaxPro让.NET的AjaxPro变得简单
c# 实现Word联接Excel的MailMerge功能
解开Ajax技术中的达芬奇密码
专家讲解用.NET编写串口程序的一点心得
利用AJAX和ASP.NET实现简单聊天室
如何快速捕获.NET代码中隐藏的BUG
动态网页原理/.net面面观
从N层到.NET详细剖析原理(2)
从N层到.NET详细剖析原理(1)
ASP.NET效率陷阱之——Attributes
在ASP.NET 2.0中建立站点导航层次(5)
在ASP.NET 2.0中建立站点导航层次(4)
在ASP.NET 2.0中建立站点导航层次(3)
在ASP.NET 2.0中建立站点导航层次(2)
在ASP.NET 2.0中建立站点导航层次(1)
动态网站Web开发PHP、ASP还是ASP.NET(2)
动态网站Web开发PHP、ASP还是ASP.NET(1)
让Apache支持ASP.NET-Apache,ASP.NET
.Net下的数据备份和还原

ASP.NET 中的 官网 Ext direct包中.NET版的问题


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-13   浏览: 210 ::
收藏到网摘: n/a

下载了官网的 Ext direct 包进行研究,发现服务器端返回结果存在一点小问题。 主要问题在返回的结果 result 标记对应的数据是字符串,请看以下官方例子中返回的数据:
复制代码 代码如下:

{"type":"rpc","tid":2,"action":"Sample","method":"SaveForm","result":"{\"firstName\":\"4\",\"lastName
\":\"4\",\"age\":4}"}

“ result ”标记对应的是一个字符串,而不是对象,这就需要在处理数据时先要将字符串转换成 JSON 对象才能继续处理。这会造成使用 DirectStore 作为 Grid 数据源时取不到数据的问题。在官网论坛找了一下,有个例子是重写 Ext.data.DirectProxy 的 createCallback 方法实现的,其目的就是在获取到数据后,将 result 中的数据转换为对象再返回数据。以下是重写 createCallback 方法的代码:
复制代码 代码如下:

Ext.override(Ext.data.DirectProxy, {
createCallback: function(action, reader, cb, scope, arg) {
return {
callback: (action == 'load') ? function(result, e) {
if (typeof result == 'string') {
result = Ext.decode(result);
}
if (!e.status) {
this.fireEvent(action + "exception", this, e, result);
cb.call(scope, null, arg, false);
return;
}
var records;
try {
records = reader.readRecords(result);
}
catch (ex) {
this.fireEvent(action + "exception", this, e, result, ex);
cb.call(scope, null, arg, false);
return;
}
this.fireEvent(action, this, e, arg);
cb.call(scope, records, arg, true);
} : function(result, e) {
if (typeof result == 'string') {
result = Ext.decode(result);
}
if (!e.status) {
this.fireEvent(action + "exception", this, e);
cb.call(scope, null, e, false);
return;
}
this.fireEvent(action, this, result, e, arg);
cb.call(scope, result, e, true);
},
scope: this
}
}
});

例子可以到以下地址下载: http://wt.ruanchen.com/"codetitle">复制代码 代码如下:

if (response.Result.GetType().ToString() == "Newtonsoft.Json.Linq.JObject" )
{
JObject o = new JObject (
new JProperty ("type" ,response.Type),
new JProperty ("tid" ,response.TransactionId),
new JProperty ("action" ,response.Action),
new JProperty ("method" ,response.Method),
new JProperty ("result" ,(JObject )response.Result)
);
return o.ToString();
}
else
{
return JsonConvert .SerializeObject(response);
}

其作用就是如果“ Result ”属性中的数据是“ JObject ”对象,程序就重新构造一个 JObject 对象再组合成字符串返回,如果不是就按原方法返回。
在客户端调用方法中只要返回一个 JObject 对象就可以了,例子如下:
复制代码 代码如下:

[DirectMethod ]
public object GetGridDatas()
{
JArray ja = new JArray ();
for (int i = 0; i < 2; i++)
{
ja.Add(new JObject (
new JProperty ("id" ,i),
new JProperty ("title" ,"title" +i.ToString())
));
}
JObject o = new JObject (
new JProperty ("results" , 2),
new JProperty ("rows" , ja)
);
return o;
}

评论 (0) All

登陆 还没注册?