1. #1
    Ext User
    Join Date
    Aug 2007
    Posts
    23
    Vote Rating
    0
    photoz is on a distinguished road

      0  

    Default [need help] Loading a TreePanel with C# webservice

    [need help] Loading a TreePanel with C# webservice


    I almost have this working, but am stuck getting proper JSON response from my C# webservice. I have been working off of this thread to get this far:
    http://extjs.com/forum/showthread.ph...t=asp.net+json

    Here is my JS:
    Code:
                Ext.Ajax.on('beforerequest', function(conn, options){
    	            conn.defaultHeaders = {'Content-Type': 'application/json; charset=utf-8'}
                });
    var Tree = Ext.tree;
                
                var tree = new Tree.TreePanel('west-div', {
                    animate:true,
                    loader: new Tree.TreeLoader(),
                    enableDD:true,
                    containerScroll: true
                });
                
                var root = new Tree.AsyncTreeNode({
                    text: 'Instrument',
                    draggable: false,
                    loader: new Tree.TreeLoader({
                        dataUrl:'../ws/Treeloading.asmx/GetNodes'
                    }),
                    id:'source'
                });
                tree.setRootNode(root);
                tree.render();
                root.expand();
    Here is my WS (just test code for now):
    Code:
    [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public TreeNode GetNodes() {
            TreeNode mynode = new TreeNode();
            mynode.id = "test";
            mynode.text = "Test node";
            mynode.leaf = true;
            mynode.cls = "file";
    
            //System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            //return js.Serialize(mynode);
            return mynode;
        }
    Currently, my WS is returning this output which is response type 'text/xml':
    Code:
    <?xml version="1.0" encoding="utf-8"?>
    
    <TreeNode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    
     xmlns="http://tempuri.org/">
    
      <text>Test node</text>
    
      <id>test</id>
    
      <leaf>true</leaf>
    
      <cls>file</cls>
    
    </TreeNode>
    Here are my ideas as to why this is failing:
    When the TreeLoader calls the WS, it does a post and sets the content-type to (from Firebug):
    Code:
    Content-Type:application/x-www-form-urlencoded,application/json
    You can see that the application/json is tacked on after the form encoding.

    So it seems that sending form data in the request (and thus setting the incorrect Content-Type) is causing .NET to send back XML instead of JSON.

    I can make a call to the WS via 'Atlas' just fine like so:
    Code:
    Treeloading.GetNodes(null, null ,null);
    And this will return the proper JSON response:
    Code:
    {"__type":"TreeNode","text":"Test node","id":"test","leaf":true,"cls":"file"}
    But the Atlas request has the proper Content-Type of only:
    Code:
    application/json; charset=utf-8
    I'm stuck, could use some help. Thanks!

  2. #2
    Ext User
    Join Date
    Aug 2007
    Posts
    23
    Vote Rating
    0
    photoz is on a distinguished road

      0  

    Default


    I have attached a project for testing, you will need to supply the ext-1.1 folder into the webroot.

    If anyone can shed some light on how to get that webservice to spit out JSON instead of XML, I'd really appreciate it!
    Attached Files

  3. #3
    Ext User
    Join Date
    Aug 2007
    Posts
    23
    Vote Rating
    0
    photoz is on a distinguished road

      0  

    Default


    bump for some love.

  4. #4
    Ext User
    Join Date
    Aug 2007
    Posts
    23
    Vote Rating
    0
    photoz is on a distinguished road

      0  

    Default


    From what I can tell, what is happening is that Ext is sending the request with the incorrect Content-Type. When I change the defaultHeaders to application/json, its still sending form-urlencoded in there with it.

    This only seems to be happening with the TreePanel, because in this thread I was using to come up with this code, they were doing an HttpProxy and changing the defaultHeaders, which appears to work there.

    Is this a possible bug with the TreePanel?

    Still looking for some help on this. Thanks for your time.

  5. #5
    Ext User
    Join Date
    Aug 2007
    Posts
    23
    Vote Rating
    0
    photoz is on a distinguished road

      0  

    Default


    I'll continue replying to myself, its fun and exciting.

    I did manage to force Ext to force the Content-type to only be application/json.

    Code:
                Ext.lib.Ajax.useDefaultHeader = false;
                Ext.Ajax.on('beforerequest', function(conn, options){
    	            conn.defaultHeaders = {'Content-Type': 'application/json; charset=utf-8'}
                });
    This got me half way there.

    After doing that, the next problem is that the POST data that is sent (node=currentNode) is sent as a name-value pair and not in JSON format, which causes the C# webservice to 500 on the request.

    So in the end I broke down and gave up on the webservice model and went with writing a *gasp* webpage that clears the response buffer, resets the content-type to application/json and sends back the JSON object.

    This solution is not as elegant, but at this point, I just need something that freaking works.

    If at some point in the future the TreeLoader can be made to make a JSON request as opposed to the traditional form-POST request, I could go back to the webservice model and let C# wrap my objects for me in JSON.

  6. #6
    Ext User
    Join Date
    Jan 2009
    Posts
    3
    Vote Rating
    0
    MarcusGarvey'sGhost is on a distinguished road

      0  

    Default


    I am having exactly the same problem as well, having to now use Response Page model within .Net to get jason back which is a very horrible approach as I’m having to split my logic across two models. Would be handy if this get's fixed in an upcoming release. Many thanks guys.

  7. #7
    Sencha User
    Join Date
    Jul 2008
    Posts
    44
    Vote Rating
    0
    Vaevictus is on a distinguished road

      0  

    Default


    got the same problem.

    would be nice if the treelaoder accepted config options such as

    dataUrl: 'WebService.asmx/GetTreeNodes',
    method: 'post',
    jsonData: { somevar: somevalue },
    headers: { 'Content-Type': 'application/json; charset=utf-8;' }

    treeloaders ajax request is not consistent with the rest of extjs ajax requests

  8. #8
    Sencha User
    Join Date
    Jul 2008
    Posts
    44
    Vote Rating
    0
    Vaevictus is on a distinguished road

      0  

    Default


    This thread should be in the 3.x forum I think.......