我正在尝试绑定数据库中的用户名,但有些是如何显示奇怪的错误。
ASPX文件
<script type="text/javascript">
$(document).ready(function () {
SearchText();
});
function SearchText() {
$(".autosuggest").autocomplete({
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/GetAutoCompleteData",
data: "{'username':'" + document.getElementById('txtSearch').value + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert('error');
}
});
}
});
}
Code Behind就像
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)> _
<WebMethod()> _
Public shared Function GetAutoCompleteData(ByVal username As String) As List(Of String)
Dim result As New List(Of String)()
Using con As New SqlConnection("Data Source=foo; Initial Catalog=db;user id=foo; password=foo;")
Using cmd As New SqlCommand("select cust_name from customers where cust_name LIKE '%'+@SearchText+'%'", con)
con.Open()
cmd.Parameters.AddWithValue("@SearchText", username)
Dim dr As SqlDataReader = cmd.ExecuteReader()
While dr.Read()
result.Add(dr("cust_name").ToString())
End While
Return result
End Using
End Using
End Function
我试过检查萤火虫。净XHR。在Post标签中显示
JSON
username
"j"
Source
{'username':'j'}
在“响应”选项卡中显示:
Server Error in '/' Application.
Unknown web method GetAutoCompleteData.
Parameter name: methodName
堆栈追踪:
[ArgumentException: Unknown web method GetAutoCompleteData.
Parameter name: methodName]
System.Web.Script.Services.WebServiceData.GetMethodData(String methodName) +178
System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs) +204
System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +136
System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +69
请注意,完全相同的代码适用于本地计算机,但不适用于生产服务器。 在控制台中显示
"NetworkError: 500 Internal Server Error - http://foo.com/Default.aspx/GetAutoCompleteData"
我无法理解我哪里出错了。它显示来自上面的javascript的错误警报。 任何人都可以帮忙吗?
答案 0 :(得分:0)
以下是我的工作代码,将其用于比较目的,它在生产中也适用于我,这可能对您有所帮助。检查ajax中的数据格式。
$( "#txt_item").autocomplete({
delay: 10,
minLength: 1,
source: function(request, response) {
$("#txt_item").addClass("ui-autocomplete-loading");
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "cms/cms_gallery.aspx/GetAutoCompleteData",
data: '{term: "' + $("#txt_item").val() + '" }',
dataType: "json",
success: function(data) {
response(data.d);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
$("#txt_item").removeClass("ui-autocomplete-loading");
alert(textStatus);
},
search: function () {
$("#txt_item").addClass("ui-autocomplete-loading");
},
response: function () {
$("#txt_item").removeClass("ui-autocomplete-loading");
}
});
}
});
COED背后:
[WebMethod]
public static List<string> GetAutoCompleteData(string term)
{
List<string> result = new List<string>();
OdbcConnection conn = new OdbcConnection(DB.DatabaseConnString());
string query = "SELECT * from gallery WHERE (unique_no LIKE '%" + My_Helper.FormatStringforDB(term) + "%') limit 5;";
if (conn.State == ConnectionState.Open)
conn.Close();
conn.Open();
OdbcDataReader dataReader = new OdbcCommand(query, conn).ExecuteReader();
if ((dataReader != null) && (dataReader.HasRows))
{
while (dataReader.Read())
{
result.Add(dataReader["unique_no"].ToString());
}
}
dataReader.Close();
return result;
}