pcirella / dynatree

Automatically exported from code.google.com/p/dynatree
0 stars 0 forks source link

Allow direct data consumption when calling .Net ASPX WebMethod #202

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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);
}

Original issue reported on code.google.com by nguyenph...@gmail.com on 27 May 2011 at 7:03

GoogleCodeExporter commented 9 years ago
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
                    });
                }
            }

Original comment by moo...@wwwendt.de on 29 May 2011 at 5:57

GoogleCodeExporter commented 9 years ago
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")).

Original comment by moo...@wwwendt.de on 31 May 2011 at 5:36

GoogleCodeExporter commented 9 years ago
"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-34
045

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

Original comment by nguyenph...@gmail.com on 2 Jun 2011 at 1:57

GoogleCodeExporter commented 9 years ago
Phi, 
I checked in yout change. Could you pleas test the version from the trunk?
Thanks

Original comment by moo...@wwwendt.de on 5 Jun 2011 at 5:11

GoogleCodeExporter commented 9 years ago
Release 1.1.2 is rather a feature release 1.2.0

Original comment by moo...@wwwendt.de on 6 Jun 2011 at 5:34

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 3 Jul 2011 at 12:49

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 16 Aug 2011 at 7:35

GoogleCodeExporter commented 9 years ago
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

Original comment by AzJ...@gmail.com on 26 Nov 2011 at 8:07

GoogleCodeExporter commented 9 years ago
Thank you Joel, for tracking that down!
I apply your patch for the next release :)

Original comment by moo...@wwwendt.de on 27 Nov 2011 at 3:34

GoogleCodeExporter commented 9 years ago
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

Original comment by grant.mc...@gmail.com on 24 Feb 2012 at 10:14

GoogleCodeExporter commented 9 years ago
This issue was closed by revision r595.

Original comment by moo...@wwwendt.de on 25 Feb 2012 at 2:06

GoogleCodeExporter commented 9 years ago
considered verified

Original comment by moo...@wwwendt.de on 17 Jul 2012 at 4:16

GoogleCodeExporter commented 9 years ago

Original comment by moo...@wwwendt.de on 17 Jul 2012 at 4:19