| Issue 202: | Allow direct data consumption when calling .Net ASPX WebMethod | |
| 1 person starred this issue and may be notified of changes. | Back to list |
What new or enhanced feature are you proposing?
When using initAjax to call a WebMethod on an .Net Aspx page, the JSON data returned is wrapped inside a "d" property. Adding a simple check and set would allow direct consumption of this data without jumping through hoops to make it work (which took me more than a day!).
What goal would this enhancement help you achieve?
Allow dynatree to work with .Net Aspx WebMethod directly.
Original code:
if( options.postProcess ){
data = options.postProcess.call(this, data, this.dataType);
}
if(!$.isArray(data) || data.length !== 0){
self.addChild(data, null);
}
Updated code:
if( options.postProcess ){
data = options.postProcess.call(this, data, this.dataType);
}
// process ASPX WebMethod JSON object inside "d" property
else if (data && data.d) {
data = data.d;
}
if(!$.isArray(data) || data.length !== 0){
self.addChild(data, null);
}
May 30, 2011
data.d was introduced with ASP.NET 3.5. This posting http://encosia.com/never-worry-about-asp-net-ajaxs-d-again/ shows how the feature could be implemented (using data.hasOwnProperty("d")).
Status:
Accepted
Jun 1, 2011
"d" is a property wrapper for data returned from a Ajax call to ASPX web page's WebMethod in .Net Framework 3.5. It is the only property exposed. Here is a clearer explaination for the "d" property: http://encosia.com/a-breaking-change-between-versions-of-aspnet-ajax/#comment-34045 I did read the doc on the onLazyRead. Doesn't it work for individual node? I was looking for loading the whole tree at once. Phi
Jun 5, 2011
Phi, I checked in yout change. Could you pleas test the version from the trunk? Thanks
Jun 5, 2011
Release 1.1.2 is rather a feature release 1.2.0
Labels:
Milestone-Release1.2
Jul 3, 2011
(No comment was entered for this change.)
Status:
Waiting
Aug 16, 2011
(No comment was entered for this change.)
Status:
Fixed
Nov 26, 2011
Hi Martin, You asked that I update this issue with the problems I encountered. My environment is ASP.NET/VB 4.0 under IIS. I set up PageMethods that are called from your dynaTree which were failing with "Invalid Type" while trying to retrieve tree nodes.
I ended up modifying your source where around line 1730 where it states:
else if (data && data.hasOwnProperty("d")) {
data = data.d;
}
to
else if (data && data.hasOwnProperty("d")) {
data = $.parseJSON(data.d); // was data = data.d;
}
I have done a bit of research since then to understand why. What I found is that our PageMethods (web services) seem to return in data.d is a string and not an object. data is an object but data.d is a string which needs to be objectified so I added the $.parseJSON call to do that.
I'm not yet sure why the response in data.d is a string at this point but may come down to some asp.net configuration settings, however this case can obviously occur and should be handled.
I have updated the your code since then to:
else if (data && data.hasOwnProperty("d")) {
data = (typeof data.d) == 'string' ? $.parseJSON(data.d) : response.d;
}
so that it will only ojbjectify it if needed as I hope to figure out how to get the service to send proper json in data.d somehow.
Btw, microsoft wraps it's returned json in data.d in an effort to prevent cross-site-scripting attacks. That's all I know about that.
Hope this helps!
Joel
Nov 27, 2011
Thank you Joel, for tracking that down! I apply your patch for the next release :)
Feb 24, 2012
It looks to me like this should read:
else if (data && data.hasOwnProperty("d")) {
data = (typeof data.d) == 'string' ? $.parseJSON(data.d) : data.d;
}
Change "response.d" to "data.d".
I was having trouble with this and made the change locally. My ASP.NET WebMethod is now consumed successfully.
Grant
Feb 25, 2012
This issue was closed by revision r595.
Jul 17, 2012
considered verified
Jul 17, 2012
(No comment was entered for this change.)
Status:
Verified
|
I don't know much about Aspx. Why does it use data.d? Are there other additional properties in data except for 'd'? Your propsal looks very simple, but I am a little bit hesitant, since there are some other frameworks out there. If you look at 'Loading custom formats' in the docs, do you think that would solve your problem? Maybe something like this could work: onLazyRead: function(node){ $.ajax({ url: […], success: function(data, textStatus){ if(data.status == "ok"){ node.setLazyNodeStatus(DTNodeStatus_Ok); node.addChild(data.d); }else{ // Server returned an error condition: set node status accordingly node.setLazyNodeStatus(DTNodeStatus_Error, { tooltip: data.faultDetails, info: data.faultString }); } }Labels: Milestone-Release1.1.2