如何在经典 ASP 中返回 JSON 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6977261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return a JSON object in classic ASP
提问by Flash
I want to return a JSON object using a classic ASP script (it's part of an AJAX request).
我想使用经典的 ASP 脚本(它是 AJAX 请求的一部分)返回一个 JSON 对象。
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/#howtoto 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']
自动完成不起作用。如果我使用像lookup这样的本地查找数组,它会起作用:['January', 'February', 'March', 'April', 'May']
But there's something wrong with the ajax meaning it doesn't return the list properly.
但是 ajax 有问题,这意味着它没有正确返回列表。
回答by Joe Enos
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""] }")
%>
回答by Jon P
Joe's answershould work for you. However you might want to look at aspjsonif you are going to be outputting a lot of JSON from classic ASP.
回答by Richard
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

