問題描述
我想使用經典的 ASP 腳本返回一個 JSON 對象(它是 AJAX 請求的一部分).
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'] }")
這會起作用嗎,還是我真的需要一個 JSON 庫?
will this work, or do I actually need a JSON library?
我正在嘗試在 http://www.devbridge.com/projects/autocomplete/jquery/#howto 工作.
I'm trying to get the autocomplete plugin at http://www.devbridge.com/projects/autocomplete/jquery/#howto to work.
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); },
});
平均售價:
<%
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']
但是 ajax 有問題,這意味著它不能正確返回列表.
But there's something wrong with the ajax meaning it doesn't return the list properly.
推薦答案
好像是客戶端解析錯誤.
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.
記得把你的雙引號加倍(至少我認為你在 VBScript 中是這樣做的——已經很久了).
Remember to double your double-quotes (at least I think that's how you do it in VBScript - been a long time).
所以:
<%
Response.ContentType = "application/json"
Response.Write("{ ""query"":""Li"", ""suggestions"":[""Liberia"",""Libyan Arab Jamahiriya"",""Liechtenstein"",""Lithuania""], ""data"":[""LR"",""LY"",""LI"",""LT""] }")
%>
這篇關于如何在經典 ASP 中返回 JSON 對象的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!