I want to return a JSON object using a classic ASP script (it's part of an AJAX request).
我想使用经典的ASP脚本返回一个JSON对象(它是AJAX请求的一部分)。
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?
这会工作,还是我真的需要一个JSON库?
Edit: I'm trying to get the autocomplete plugin at http://www.devbridge.com/projects/autocomplete/jquery/#howto to work.
编辑:我正在尝试在http://www.devbridge.com/projects/autocomplete/jquery/#howto上获取自动完成插件。
javascript:
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:
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']
自动填充功能无效。如果我使用像查找这样的本地查找数组:['January','February','March','April','May']
But there's something wrong with the ajax meaning it doesn't return the list properly.
但是ajax有问题意味着它没有正确返回列表。
3 个解决方案
#1
24
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).
记得加倍你的双引号(至少我认为你是如何在VBScript中做的 - 已经很长时间了)。
So:
所以:
<%
Response.ContentType = "application/json"
Response.Write("{ ""query"":""Li"", ""suggestions"":[""Liberia"",""Libyan Arab Jamahiriya"",""Liechtenstein"",""Lithuania""], ""data"":[""LR"",""LY"",""LI"",""LT""] }")
%>
#2
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
#3
2
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.
乔的回答应该适合你。但是,如果要从经典ASP输出大量JSON,您可能需要查看aspjson。
#1
24
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).
记得加倍你的双引号(至少我认为你是如何在VBScript中做的 - 已经很长时间了)。
So:
所以:
<%
Response.ContentType = "application/json"
Response.Write("{ ""query"":""Li"", ""suggestions"":[""Liberia"",""Libyan Arab Jamahiriya"",""Liechtenstein"",""Lithuania""], ""data"":[""LR"",""LY"",""LI"",""LT""] }")
%>
#2
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
#3
2
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.
乔的回答应该适合你。但是,如果要从经典ASP输出大量JSON,您可能需要查看aspjson。