17

I want to return a JSON object using a classic ASP script (it's part of an AJAX request).

If I just send the reponse as text like:

response.write("{ query:'Li', suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'], data:['LR','LY','LI','LT'] }")

will this work, or do I actually need a JSON library?

Edit: I'm trying to get the autocomplete plugin at http://www.devbridge.com/projects/autocomplete/jquery/#howto to work.

javascript:

 $(document).ready(function() {
    var a = $('#txtValue').autocomplete({ 
    serviceUrl:'script.asp',
    minChars:2, 
    maxHeight:400,
    width:300,
    zIndex: 9999,
    deferRequestBy: 0, //miliseconds
    onSelect: function(value, data){ alert('You selected: ' + value + ', ' + data); },
});

ASP:

<% 
response.ContentType = "application/json"
response.write("{ query:'Li', suggestions:['Liberia','Libyan Arab Jamahiriya','Liechtenstein','Lithuania'], data:['LR','LY','LI','LT'] }") 
%>

Autocomplete is not working. It works if I use a local lookup array like lookup: ['January', 'February', 'March', 'April', 'May']

But there's something wrong with the ajax meaning it doesn't return the list properly.

Flash
  • 215
  • 1
  • 2
  • 6
  • Have you actually tried it? Give it a go, what have you got to lose? – Jon P Aug 08 '11 at 02:35
  • I tried it, it's not working haha. I'm just not sure if this is the reason or not. I'm trying to get the autocomplete plugin (http://www.devbridge.com/projects/autocomplete/jquery/#howto) working – Flash Aug 08 '11 at 02:38
  • I know you already solve your problem, but here is a link to validate your JSON and be sure that is not it that cause the problem: http://jsonlint.com/ – Onaiggac Jul 24 '14 at 11:50

3 Answers3

34

It appears to be a parsing error on the client side.

I didn't think this would make a difference, but it looks like if you quote everything, including the property names, it seems to work. And use double-quotes instead of single quotes - that apparently is making a difference.

Remember to double your double-quotes (at least I think that's how you do it in VBScript - been a long time).

So:

<%
    Response.ContentType = "application/json"
    Response.Write("{ ""query"":""Li"", ""suggestions"":[""Liberia"",""Libyan Arab Jamahiriya"",""Liechtenstein"",""Lithuania""], ""data"":[""LR"",""LY"",""LI"",""LT""] }")
%>
Joe Enos
  • 39,478
  • 11
  • 80
  • 136
  • Beat me to it. Yes double your quotes to escape them in vbScript. – Jon P Aug 08 '11 at 03:08
  • 3
    It's not a parsing error. The [JSON grammar](http://json.org/) specifies that property names are strings, and strings must be enclosed in double quotes. – Cheran Shunmugavel Dec 11 '11 at 06:37
  • I don't see why that wouldn't work. Just make sure you set the content type to "application/json". – Joe Enos Aug 08 '11 at 02:36
  • In the documentation for the plugin, it says: "Web page that provides data for Ajax Autocomplete will receive GET request with querystring ?query=Li, and it must return JSON data in the following format" and then gives string in my question. Shouldn't it work for testing if I just response.write it as shown? – Flash Aug 08 '11 at 02:41
  • I added response.ContentType = "application/json" but it's still not working :( – Flash Aug 08 '11 at 02:46
  • Use Response.Clear before setting content-type for it to work in IE 8 or under – Yogesh Jindal May 08 '14 at 21:53
3

Joe's answer should work for you. However you might want to look at aspjson if you are going to be outputting a lot of JSON from classic ASP.

Community
  • 1
  • 1
Jon P
  • 19,442
  • 8
  • 49
  • 72
  • It's also straightforward to use the json2.js script, from Crockford. https://github.com/douglascrockford/JSON-js/blob/master/json2.js See also http://stackoverflow.com/a/9630663/48082 – Cheeso Mar 26 '12 at 18:12
3

I got it to work with the code below.... After doubling the quotes and putting in the query string

currQuery= request.querystring("query")
response.expires=-1
Dim rsMain,sqlMain,rettxt,JobOpenToArr
set rsMain= Server.CreateObject("ADODB.Recordset")
rsMain.CursorLocation = adUseClient
sqlMain = "select JobOpenTo FROM Jobs WHERE JobOpenTo LIKE '%"&currQuery & "%' group by JobOpenTo order by JobOpenTo" 
rsMain.Open sqlMain, Session("XXX_CMS")
if Not rsMain.Eof  Then   
              '## build the string
       rettxt = "{query:""" & currQuery & """, suggestions:["

     JobOpenToArr = rsMain.getRows()     
     For i = 0 to UBound(JobOpenToArr,2)

       rettxt = rettxt & """" & JobOpenToArr(0,i) & ""","

     Next    
     '##knock off trailing comma
     rettxt = left(rettxt,len(rettxt)-1)
     rettxt = rettxt & "]}"
     Response.Write rettxt 
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
Richard
  • 224
  • 1
  • 7
  • 18