JavaScript自动完成数组不适用于vb.net中的WebMethod
JavaScript中的自动完成文本框代码JavaScript自动完成数组不适用于vb.net中的WebMethod,javascript,vb.net,webmethod,Javascript,Vb.net,Webmethod,JavaScript中的自动完成文本框代码 function getList_FixedValue() { var arr = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"]; return arr; } function getList_FromServerSide() { $.ajax({ type: "POST", url: "patient-pr
function getList_FixedValue() {
var arr = ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby"];
return arr;
}
function getList_FromServerSide() {
$.ajax({
type: "POST",
url: "patient-problem-submit.aspx/GetCCList",
data: '{doctorId: "' + $("#<%=hdnDoctorId.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d); //here alert shows my expected data
return response.d;
},
failure: function (response) {
alert("failed to get data");
}
});
}
$('#txtCCPick').autocompleteArray(
//getList_FixedValue(), //this works fine
getList_FromServerSide(), //this not working
{
delay: 10,
minChars: 1,
matchSubset: 1,
onItemSelect: selectItem,
onFindValue: findValue,
autoFill: true,
maxItemsToShow: 10
}
);
});
VB中的WebMethod
<System.Web.Services.WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
Public Shared Function GetCCList(ByVal doctorId As String) As String()
Dim customers As New List(Of String)()
Using conn As New SqlConnection()
conn.ConnectionString = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString
Using cmd As New SqlCommand()
cmd.CommandText = String.Format("SELECT pkCCID, CCName FROM CC WHERE fkDoctorID = " + doctorId + " order by CCName")
cmd.CommandType = CommandType.Text
'cmd.Parameters.AddWithValue("@DoctorId", doctorId)
cmd.Connection = conn
conn.Open()
Dim dbAdpt As New SqlDataAdapter(cmd)
Dim ds As New DataSet
dbAdpt.Fill(ds)
If (Not ds Is Nothing) Then
If (ds.Tables(0).Rows.Count > 0) Then
For Each row As DataRow In ds.Tables(0).Rows
customers.Add(String.Format("{0}", row("CCName")))
Next
End If
End If
conn.Close()
End Using
End Using
Return customers.ToArray()
End Function
在autocompleteArray中,当我调用getList_FixedValue函数时,项目被正确地加载到我的文本框中。但是,当我从服务器端调用getList_函数时,项目不会加载到我的文本框中。所以我需要帮助解决这个问题。提前感谢。最终我通过以下方式得到了解决方案。问题是ajax调用的成功部分并没有直接返回选项
function getList_FromServerSide() {
var result;
$.ajax({
type: "POST",
url: "patient-problem-submit.aspx/GetCCList",
data: '{doctorId: "' + $("#<%=hdnDoctorId.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
async: !1,
success: function (response) {
//alert(response.d); //here alert shows my expected data
result = response.d;
},
failure: function (response) {
alert("failed to get data");
}
});
return result;
}
您可以在alert:alertresponse.d;中看到什么结果;?警报显示了一些以逗号分隔的项目。您似乎是在直接推送json字符串,而不是传递值数组。尝试类似-var arr=jQuery.parseJSONjson_text这样的方法,然后传递数组。这有助于了解JSON文件的内容/格式。我在success函数中尝试了两行代码,但不起作用-var arr=[c++,java,php,coldfusion,javascript,asp,ruby];返回arr;