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

本文介绍了解决 Ext.Direct 服务器端返回 JSON 数据格式问题的方法,通过修改 DirectProcessor 类来确保返回的对象而非字符串,简化客户端数据处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<!-- [if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:PunctuationKerning/> <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing> <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery> <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery> <w:ValidateAgainstSchemas/> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:Compatibility> <w:SpaceForUL/> <w:BalanceSingleByteDoubleByteWidth/> <w:DoNotLeaveBackslashAlone/> <w:ULTrailSpace/> <w:DoNotExpandShiftReturn/> <w:AdjustLineHeightInTable/> <w:BreakWrappedTables/> <w:SnapToGridInCell/> <w:WrapTextWithPunct/> <w:UseAsianBreakRules/> <w:DontGrowAutofit/> <w:UseFELayout/> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument> </xml><![endif]--><!-- [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles> </xml><![endif]--> <!-- [if gte mso 10]> <mce:style><!-- /* Style Definitions */ table.MsoNormalTable {mso-style-name:普通表格; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Times New Roman"; mso-fareast-font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} table.MsoTableGrid {mso-style-name:网格型; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; border:solid windowtext 1.0pt; mso-border-alt:solid windowtext .5pt; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-border-insideh:.5pt solid windowtext; mso-border-insidev:.5pt solid windowtext; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.0pt; font-family:"Times New Roman"; mso-ansi-language:#0400; mso-fareast-language:#0400; mso-bidi-language:#0400;} --> <!-- [endif]-->

下载了官网的 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://ny504427.bizhostnet.com/Default.aspx

不过笔者的想法是能不能在服务器端解决这个问题。在 Ext.Direct.dll 的源代码中,笔者找到 DirectResponse 类的“ Result ”的定义类型是一个 object ,于是笔者在例子中将客户端调用的方法的返回数据类型修改为 JObject ,但是在执行以下语句的时候会出现错误:

return JsonConvert .SerializeObject(response);

看来这里需要修改一下,于是笔者将 DirectProcessor 类中的以上这句修改为以下代码:

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();

其作用就是如果“ 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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值