Questions about. net in the ext direct package on the official website

Source: Internet
Author: User

The main problem is that the returned result indicates that the corresponding data is a string. See the data returned in the following official example: CopyCode The Code is as follows: {"type": "RPC", "TID": 2, "action": "sample", "method": "saveform", "result ": "{\" firstname \ ": \" 4 \ ", \" lastname
\ ": \" 4 \ ", \" Age \ ": 4 }"}

The "result" Mark corresponds to a string instead of an object. Therefore, you must convert the string to a JSON object before processing the data. This causes the problem that data cannot be obtained when directstore is used as the grid data source. I found it on the official website forum. An example is to rewrite Ext. data. the createcallback method of directproxy is implemented to convert the data in the result into an object and then return the data after obtaining the data. The following code overrides the createcallback method: Copy code The Code is 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
}
}
});

the example can be downloaded from the following address: http://xiazai.jb51.net/200906/yuanma/Direct.rar
but the author's idea is whether the problem can be solved on the server side. In Ext. direct. in DLL Source Code , the author finds that the "result" of the directresponse class is an object, in this example, the author changes the returned data type of the method called by the client to jobject, but an error occurs when the following statement is executed:
return jsonconvert. serializeobject (response);
it seems that you need to modify it here, so I changed the above sentence in the directprocessor class to the following code: copy Code the code is as follows: if (response. result. getType (). tostring () = "newtonsoft. JSON. LINQ. jobject ")
{< br> 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 ();
}< br> else
{< br> return jsonconvert. serializeobject (response);
}

If the data in the "result" attribute is a "jobject" object, Program re-constructs a jobject object and then groups the strings to return the result. If not, return the result based on the original method.
you only need to return a jobject object in the client call method. The example is as follows: copy Code the code is as follows: [directmethod]
Public object getgriddatas ()
{< br> jarray ja = new jarray ();
for (INT I = 0; I <2; I ++)
{< br> Ja. add (New jobject (
New jproperty ("ID", I),
New jproperty ("title", "title" + I. tostring ()
);
}< br> jobject o = new jobject (
New jproperty ("Results", 2 ),
New jproperty ("rows", ja)
);
return O;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.