madsovenielsen Posted January 10, 2011 Share Posted January 10, 2011 Hello everyone.Im trying to send a JSON string from a Classic ASP page over HTTP using xmlhttprequest. The string, as far as i can see, is being sendt fine. The formatting is fine. Its valid JSON. The JSON string from lookup.asp { "suggestResults" : [ {'name':'Madsen'},{'name':'Mads'},{'name':'Madsine'},{'name':'Mads-Emil'},{'name':'Madsbjerg'},{'name':'Madsbøll'},{'name':'Mads-Peter'},{'name':'Madsen-Østerbye'},{'name':'Mads-Christian'},{'name':'Madslund'},{'name':'dummy'}]}; Client side script <script type="text/javascript"> function getData() { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { // var json = { "suggestResults" : [ {'name':'Madsen'},{'name':'Mads'},{'name':'Madsine'},{'name':'Mads-Emil'},{'name':'Madsbjerg'},{'name':'Madsbøll'},{'name':'Mads-Peter'},{'name':'Madsen-Østerbye'},{'name':'Mads-Christian'},{'name':'Madslund'},{'name':'dummy'}]}; var json = xmlhttp.responseText; document.getElementById("name_dropdown").innerHTML = ""; var city_results = document.getElementById("name_dropdown"); var i = 0; while (i < 10) { city_results.innerHTML += '<div class="search_suggest_item">' + json.suggestResults[0].name + "</div>"; i++; } } } xmlhttp.open("GET", "../asp/lookup_name_company.asp?word=" + document.all.WHAT.value, true); xmlhttp.send(); }</script> We get the following error on the Client side: Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)Timestamp: Mon, 10 Jan 2011 08:39:45 UTCMessage: 'suggestResults.0' is null or not an objectLine: 122Char: 17Code: 0 But when we embed the JSON string in a local variable on the clientside, all works fine. Any help is greatly appriciated./mads Link to comment Share on other sites More sharing options...
Synook Posted January 10, 2011 Share Posted January 10, 2011 The responseText is just a string - you need to parse that string into a JSON structure using JSON.parse().P.S. using single quotes is technically invalid JSON, but JavaScript accepts it. Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 The responseText is just a string - you need to parse that string into a JSON structure using JSON.parse().P.S. using single quotes is technically invalid JSON, but JavaScript accepts it.I now use Douglas Crockford's json2.js lib.function getData() { if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function () { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { var json = xmlhttp.responseText; var test1 = JSON.stringify(json); //alert(test1); var testvar2 = JSON.parse(test1); //alert(testvar2); document.getElementById("name_dropdown").innerHTML = ""; var city_results = document.getElementById("name_dropdown"); var i = 0; while (i < 10) { city_results.innerHTML += '<div class="search_suggest_item" onclick="extractToField(this.innerHTML;)">' + testvar2.suggestResults[0].name + "</div>"; i++; } } } xmlhttp.open("GET", "../asp/lookup_name_company.asp?word=" + sWHAT, true); xmlhttp.send(); } I get the same error: Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)Timestamp: Mon, 10 Jan 2011 08:39:45 UTCMessage: 'suggestResults.0' is null or not an objectLine: 122Char: 17Code: 0 If i alert(test1); and alert(testvar2); i get valid JSON in both cases./mads Link to comment Share on other sites More sharing options...
jeffman Posted January 10, 2011 Share Posted January 10, 2011 if JSON.parse is doing its job, then alert(testvar2) would not display a json string at all. It would alert something like [Object object] Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 if JSON.parse is doing its job, then alert(testvar2) would not display a json string at all. It would alert something like [Object object]Okay. do you know what i am doing wrong ?/mads Link to comment Share on other sites More sharing options...
jeffman Posted January 10, 2011 Share Posted January 10, 2011 My guess is that calling JSON.stringify is messing things up. xmlhttp.responseText is already a string. I can't even guess what would happen to the syntax if you stringified a string. stringify is designed to turn a javascript object into a string that conforms to JSON syntax.In other words, try passing xmlhttp.responseText directly to JSON.parse Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 My guess is that calling JSON.stringify is messing things up. xmlhttp.responseText is already a string. I can't even guess what would happen to the syntax if you stringified a string. stringify is designed to turn a javascript object into a string that conforms to JSON syntax.In other words, try passing xmlhttp.responseText directly to JSON.parseIm am now passing xmlhttp.responseText directly to JSON.parse()And i get the following error:Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)Timestamp: Mon, 10 Jan 2011 17:32:52 UTCMessage: Syntax errorLine: 25Char: 13Code: 0 Link to comment Share on other sites More sharing options...
justsomeguy Posted January 10, 2011 Share Posted January 10, 2011 What does the string look like that you're trying to parse? Are you using double quotes in the string? Link to comment Share on other sites More sharing options...
thescientist Posted January 10, 2011 Share Posted January 10, 2011 I would guess that if your JSON doesn't validate here http://www.jsonlint.com/(which your's doesn't) then, you might want to consider making it double quotes, especially if you are using the JSON library. (the library and validator are both written by the same person) Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 What does the string look like that you're trying to parse? Are you using double quotes in the string? { "suggestResults" : [ {'name':'Grethe'},{'name':'Gerda'},{'name':'Gitte'},{'name':'Gudrun'},{'name':'Gunnar'},{'name':'Georg'},{'name':'Gert'},{'name':'Grete'},{'name':'Gustav'},{'name':'Gurli'},{'name':'dummy'}]} Based on this example on json.org var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ]}; http://www.json.org/js.html/mads Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 I would guess that if your JSON doesn't validate here http://www.jsonlint.com/(which your's doesn't) then, you might want to consider making it double quotes, especially if you are using the JSON library. (the library and validator are both written by the same person)I will try to use double quotes. I dont think it will be easy, we are using classic asp. so a single quote is used for comments :)/mads Link to comment Share on other sites More sharing options...
madsovenielsen Posted January 10, 2011 Author Share Posted January 10, 2011 Thanks all. Its working now. using only double quotes./mads Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.