Return 一个数组,从 VB 脚本到 JQuery in classic ASP for Autocomplete text box
Return an array from VB Script to JQuery in classic ASP for Autocomplete text box
我是 web 开发的初学者,目前正在添加一个自动完成文本框,它根据用户输入从 MS SQL 服务器检索数据。
我在 JQuery 中声明了一个名为 CodeList 的数组,并从名为 getListOfCode 的 VB 脚本函数中转换 return 代码。数组:CodeList 无法转换为函数:getListOfCode 时抛出错误。
(错误代码:ASP_0106_:_80020005|Type_Mismatch)
我试图在 VB 脚本中将函数 getListOfCode() 重新定义为数组,但没有成功。谁能给我一些建议吗?
VB 脚本部分
<%
Function getListOfCode()
Set objRec = Server.CreateObject("ADODB.Recordset")
strSQL = "Select distinct Code_A, Code_A + " & " ' : ' " & _
" + Long_Name AS Code_Name " & _
"FROM CodeMaster " & _
"Where Type = 'A' "
objrec.Open strSQL, objConnect
Dim CodeList(), i
Redim CodeList(objrec.Fields.Count - 1)
i = 0
Do Until i < objrec.Fields.Count - 1
CodeList(i) = objrec.Fields("Code_Name")
i = i + 1
objrec.MoveNext
Loop
getListOfCode = CodeList
>> To return an array to Java Script with casting.
If I write CodeList(i), it does not throw an error
which means getListOfCode is being recognized as a string
objRec.Close
Set objRec = Nothing
End Function
%>
JQuery部分
$(function() {
var CodeList = [
<%= getListOfCode() %>
];
$( "#frmCode" ).autocomplete({
source: CodeList
});
});
如有任何建议,我们将不胜感激。提前谢谢你。
只需更改此行:
getListOfCode = CodeList
至:
getListOfCode = String.Join(",", CodeList)
应该就可以了!该字符串将在 ASP 文件中呈现为原始 JSON 数组。
此外,更改此行:
CodeList(i) = objrec.Fields("Code_Name")
包含引号,以便 JS 将您的字符串解释为实际的 JS 字符串:
CodeList(i) = """" & objrec.Fields("Code_Name") & """"
我根据您的建议进行了更改,改为使用全局变量而不是强制转换为函数。
我是 web 开发的初学者,目前正在添加一个自动完成文本框,它根据用户输入从 MS SQL 服务器检索数据。
我在 JQuery 中声明了一个名为 CodeList 的数组,并从名为 getListOfCode 的 VB 脚本函数中转换 return 代码。数组:CodeList 无法转换为函数:getListOfCode 时抛出错误。 (错误代码:ASP_0106_:_80020005|Type_Mismatch)
我试图在 VB 脚本中将函数 getListOfCode() 重新定义为数组,但没有成功。谁能给我一些建议吗?
VB 脚本部分
<%
Function getListOfCode()
Set objRec = Server.CreateObject("ADODB.Recordset")
strSQL = "Select distinct Code_A, Code_A + " & " ' : ' " & _
" + Long_Name AS Code_Name " & _
"FROM CodeMaster " & _
"Where Type = 'A' "
objrec.Open strSQL, objConnect
Dim CodeList(), i
Redim CodeList(objrec.Fields.Count - 1)
i = 0
Do Until i < objrec.Fields.Count - 1
CodeList(i) = objrec.Fields("Code_Name")
i = i + 1
objrec.MoveNext
Loop
getListOfCode = CodeList
>> To return an array to Java Script with casting.
If I write CodeList(i), it does not throw an error
which means getListOfCode is being recognized as a string
objRec.Close
Set objRec = Nothing
End Function
%>
JQuery部分
$(function() {
var CodeList = [
<%= getListOfCode() %>
];
$( "#frmCode" ).autocomplete({
source: CodeList
});
});
如有任何建议,我们将不胜感激。提前谢谢你。
只需更改此行:
getListOfCode = CodeList
至:
getListOfCode = String.Join(",", CodeList)
应该就可以了!该字符串将在 ASP 文件中呈现为原始 JSON 数组。
此外,更改此行:
CodeList(i) = objrec.Fields("Code_Name")
包含引号,以便 JS 将您的字符串解释为实际的 JS 字符串:
CodeList(i) = """" & objrec.Fields("Code_Name") & """"
我根据您的建议进行了更改,改为使用全局变量而不是强制转换为函数。