jQuery Forum

Hi all, 

I've loved using jQuery these past few months, but I'm really pulling 
my hair our here. 

The following is the "endpoint" asp code that gets called to populate 
my treeview:

<% 
rootid = request.querystring("rootid") 
json = "" 
set Conn = server.CreateObject("ADODB.Connection") 
Conn.Open strProvider 
if rootid = "source" then 
        json = "[" 
        set RS = Server.CreateObject("ADODB.RecordSet") 
        Sql = "select categoryid,categoryname,(select count(*) from 
tblcategories where parentid = c1.categoryid) ChildCount from 
tblcategories c1 where parentid = 0" 
        session("errorsql") = sql 
        set Cmd = Server.CreateObject("ADODB.Command") 
        Cmd.CommandText = Sql 
        Cmd.ActiveConnection = Conn 
        rs.open Cmd 
        if not RS.EOF then 
                GotRows = "y" 
                arrResultSet = RS.GetRows() 
                numRows = ubound(arrResultSet,2) 
                For i = 0 to numRows 
                        categoryid = arrResultSet(0,i) 
                        categoryname = arrResultSet(1,i) 
                        childcount = cint(arrResultSet(2,i)) 
                        json = json & "{'text': '" & categoryname & "','id':'" & categoryid 
& "'" 
                        if childcount > 0 then 
                                json = json & ", 'hasChildren':true" 
                        end if 
                        json = json & "}" 
                        if i < numRows  then 
                                json = json & "," 
                        end if 
                Next 
        else 
                GotRows = "n" 
                json = json & "{'text': 'No Categories have been created'}" 
        end if 
        json = json & "]" 
        RS.close 
elseif isnumeric(rootid) and rootid <> "" then 
        json = "[" 
        set RS = Server.CreateObject("ADODB.RecordSet") 
        Sql = "select categoryid,categoryname,(select count(*) from 
tblcategories where parentid = c1.categoryid) ChildCount from 
tblcategories c1 where parentid = ?" 
        session("errorsql") = sql 
        set Cmd = Server.CreateObject("ADODB.Command") 
        Cmd.Parameters.Append CreateIntParameter(Cmd, "rootid", rootid) 
        Cmd.CommandText = Sql 
        Cmd.ActiveConnection = Conn 
        rs.open Cmd 
        if not RS.EOF then 
                GotRows = "y" 
                arrResultSet = RS.GetRows() 
                numRows = ubound(arrResultSet,2) 
                For i = 0 to numRows 
                        categoryid = arrResultSet(0,i) 
                        categoryname = arrResultSet(1,i) 
                        childcount = cint(arrResultSet(2,i)) 
                        json = json & "{'text': '" & categoryname & "','id':'" & categoryid 
& "'" 
                        if childcount > 0 then 
                                json = json & ", 'hasChildren':true" 
                        end if 
                        json = json & "}" 
                        if i < numRows then 
                                json = json & "," 
                        end if 
                Next 
        else 
                GotRows = "n" 
        end if 
        json = json & "]" 
        RS.close 
end if 
set Cmd = nothing 
set RS=nothing 
conn.close 
set conn=nothing 
response.write json 
%> 

This code produces the following JSON string viewable when I hit the 
page directly with the ?rootid=source parameter: 

[{'text': 'Cat1.0','id':'1'},{'text': 'Cat1.1','id':'2', 
'hasChildren':true},{'text': 'Cat1.2','id':'3'}] 

Unless I've received a severe blow to the head, I recognize this as 
valid JSON and just what the treeview needs to produce a flat display 
of 3 nodes with the second having a '+' sign for expandability. 

Here's the frustrating part, when I past that string into the body of 
my asp page, comment out the response.write line, save, and run 
everything I get the simple tree that I'd expect.  When I remove the 
JSON string, and uncomment the response.write line, nothing happens. 

I've tried flushing the response object, ending the response object, 
clearing the response before writing to it, yet nothing seems to work. 

Please, please help! 

  • No status
  • Working on it
  • Answered
  • Need more info
  1 user has this question 
In order for JSON to be valid, it must use double quotes, not single quotes.  Also, it is preferable to avoid manually concatenating JSON strings together yourself, if the option exists to serialize it in your backend.

I just replaced all single quotes with double quotes, and the JSON string that is produced is as follows:

[{"text": "Cat1.0","id":"1"},{"text": "Cat1.1","id":"2", "hasChildren":true},{"text": "Cat1.2","id":"3"}]

Unfortunately, I'm still observing the same behavior i.e. when I response.write that string, no treeview is produced, yet when I simply past that string in the body of the asp page, the tree is created as I would expect.

It really seems like this has something to do with the response object.

Thoughts anyone?
*bump*  

This code is still not working properly.  Can anyone give me a hint as to why?
It would really help to see this in action. If possible, put it online and let us check.

-- Aivo --

You will be prompted to login:

username is admin
password is pass

Now "authenticated," refresh the above link and you'll see a blank page which should, by all I can figure have a simple tree control on it.

Here is the code that sets up the treeview:

<link rel="stylesheet" href="/js/jquery-treeview/jquery.treeview.css" />
<link rel="stylesheet" href="/js/jquery-treeview/red-treeview.css" />
<link rel="stylesheet" href="/js/jquery-treeview/demo/screen.css" />

<script src="/js/jquery-treeview/lib/jquery.js" type="text/javascript"></script>
<script src="/js/jquery-treeview/lib/jquery.cookie.js" type="text/javascript"></script>
<script src="/js/jquery-treeview/jquery.treeview.js" type="text/javascript"></script>
<script src="/js/jquery-treeview/jquery.treeview.async.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#categorytree").treeview({
url: "/dbadmin/includes/incCategoryAJAX.asp"
})
});
</script>

I have a <ul> elelement with the id="categorytree" on the categories page.  Again, it works properly when I simply take the output of the incCategoryAJAX.asp page and paste it in the body of that same page, commenting out the response.write.

There has got to be something in the response.write that is not making the content available or not correctly encoding the content for the jquery code that needs to interpret the JSON string.

Your problems start from invalid xhtml. In xhtml, tag names are case sensitive. Doing this is wrong:

  1. <FORM ACTION="/members/Login_Validate.asp" METHOD="POST" name="frmLogin">

Then I see something like that returned from AJAX request:

  1. <!--[{"text": "Cat1.0","id":"1"}, {"text": "Cat1.1","id":"2", "hasChildren":true}, {"text": "Cat1.2","id":"3"}]-->
Why xml comment? That is invalid JSON. Fix and try again.

-- Aivo --
Ok, fixed those issues.  It still is not working.  Anything else it could be?
I guess I'm confused about what you are saying is incorrect.  I have commented out my response.write line in incCategoryAJAX.asp and have pasted the JSON string in the body of that asp page.

You will notice that the http://icandothisandthat.com/dbadmin/categories.asp page now correctly display a treeview control.  Mind you, the JSON pasted in the body of the incCategoryAJAX.asp page is the same JSON it produced before I commented out the response.write line.

Hopefully, this outlines my confusion and allows you to formulate the following question, "Why would the treeview work properly when the JSON produced by incCategoryAJAX.asp is pasted in the the page body, but not when the JSON content is written programmatically with a response.write?"

This is why I feel there is something weird (or at least, unknown to me) happening with the response.write.
but treeview requests it from

See the difference?
Two wrongs don't make one right in that case. Fix your url and asp page and it sould work.


-- Aivo --
I see where the miscommunication was and have fixed the problem.  As you mentioned, fixing the parameter fixed the problem.  I appreciate your time to show that to me.
Post Actions
Statistics
  • 14
     Replies
  • 98
     Views
  • 0
     Followers
Tags for the post

Edit Link Delete Link

Edit Link Delete Link

LoadingImage