The main question in the returned result mark corresponds to the data is a string, see the following official example of the data returned:
Copy Code code as follows:
{' type ': ' RPC ', ' tid ': 2, ' action ': ' Sample ', ' method ': ' Saveform ', ' result ': ' {\ ' firstname\ ': ' 4\ ', \ ' LastName
\ ": \ 4\", \ "Age\": 4} "}
The "result" tag corresponds to a string, not an object, which requires that the string be converted to a JSON object before it can be processed. This can result in the problem of not getting data when using Directstore as a Grid data source. In the official website forum to find out, there is an example is the rewrite Ext.data.DirectProxy Createcallback method to achieve, the purpose is to get the data, the result of the data into the object and then return the data. Here's the code to rewrite the Createcallback method:
Copy Code code as follows:
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
}
}
});
Examples can be downloaded to the following address: Http://xiazai.jb51.net/200906/yuanma/Direct.rar
But the author's idea is to be able to solve this problem on the server side. In the source code of Ext.Direct.dll, the author found that the definition type of "result" of Directresponse class is an object, so the author modifies the return data type of the method called by the client in the example to Jobject. However, an error occurs when you execute the following statement:
Return JsonConvert. SerializeObject (response);
It seems that there needs to be modified, so the author of the Directprocessor class in the above sentence modified to the following code:
Copy Code code as follows:
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);
}
The effect is that if the data in the "result" attribute is a "Jobject" object, the program reconstructs a Jobject object and then combines it back into a string, if not the original method.
It is OK to return only one Jobject object in the client invocation method, as shown in the following example:
Copy Code code as follows:
[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;
}