-
21 Aug 2007 9:36 AM #1
[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:
Here is my WS (just test code for now):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();
Currently, my WS is returning this output which is response type 'text/xml':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; }
Here are my ideas as to why this is failing: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>
When the TreeLoader calls the WS, it does a post and sets the content-type to (from Firebug):
You can see that the application/json is tacked on after the form encoding.Code:Content-Type:application/x-www-form-urlencoded,application/json
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:
And this will return the proper JSON response:Code:Treeloading.GetNodes(null, null ,null);
But the Atlas request has the proper Content-Type of only:Code:{"__type":"TreeNode","text":"Test node","id":"test","leaf":true,"cls":"file"}
I'm stuck, could use some help. Thanks!Code:application/json; charset=utf-8
-
21 Aug 2007 11:50 AM #2
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!
-
22 Aug 2007 5:40 AM #3
-
24 Aug 2007 5:58 AM #4
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.
-
24 Aug 2007 11:43 AM #5
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.
This got me half way there.Code:Ext.lib.Ajax.useDefaultHeader = false; Ext.Ajax.on('beforerequest', function(conn, options){ conn.defaultHeaders = {'Content-Type': 'application/json; charset=utf-8'} });
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.
-
30 Jan 2009 6:48 PM #6
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.
-
24 Jan 2010 3:34 PM #7
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
-
25 Jan 2010 9:41 AM #8
This thread should be in the 3.x forum I think.......


Reply With Quote