【发布时间】:2014-06-15 22:01:52
【问题描述】:
我正在尝试从数据库绑定用户名,但它显示出奇怪的错误。
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');
}
});
}
});
}
后面的代码就像
<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 的错误警报。 任何人都可以帮忙吗?
【问题讨论】:
-
您发送的 AJAX 中的数据格式是否正确?
-
生产服务器上是否存在方法
GetAutoCompleteData?拼写正确吗?
标签: jquery asp.net json web-services autocomplete