我使用带有ASHX的jQuery自动完成作为源代码。但是每个请求都会引发错误,编号为200。
我的JavaScript:
$(function () {
$('#username input[type = "text"]').autocomplete({
source: function (request, response) {
$.ajax({
url: "UserName.ashx?term=" + request.term,
dataType: "json",
type: "POST",
success: function (data) {
response($.map(data, function (item) {
return {
label: item.Value
}
}))
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
});
});
HTML:
<div id="username">
<asp:Label Text="User name" runat="server" />
<asp:TextBox ID="txtUserName" runat="server" />
</div>
ASHX:
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
context.Response.Clear()
context.Response.ContentType = "application/json"
Try
Dim oList As New List(Of ResultItem)
For Each File As System.IO.FileInfo In New System.IO.DirectoryInfo(ApplicationInfo.Settings.AbsoluteUserConfigPath).GetFiles(context.Request.QueryString("term") & "*.xml")
oList.Add(New ResultItem With {.Value = System.Text.RegularExpressions.Regex.Replace(File.Name, File.Extension & "$", String.Empty)})
Next
context.Response.Write(New System.Web.Script.Serialization.JavaScriptSerializer().Serialize(oList))
Catch ex As Exception
context.Response.Clear()
context.Response.ContentType = "text/plain"
context.Response.StatusCode = 500
context.Response.StatusDescription = ex.Message
End Try
End Sub
对于静态内容,它可以正常工作但是有了这个请求,我收到以下错误:SyntaxError: JSON.parse: unexpected character
。
生成的JSON看起来像
[
{"Value":"Item 1","Name":"","Description":""},
{"Value":"Item 2","Name":"","Description":""},
{"Value":"Item 3","Name":"","Description":""},
{"Value":"Item 4","Name":"","Description":""}
]
并且应该有效。
知道为什么它不起作用?什么是意外的角色?
感谢您的回复。
答案 0 :(得分:1)
我更改了源代码和JSON,然后运行它。
$(function () {
$('#username input[type = "text"]').autocomplete({
source: "UserName.ashx"
});
}
[
{"object":"Item 1","label":"","Description":""},
{"object":"Item 2","label":"","Description":""},
{"object":"Item 3","label":"","Description":""},
{"object":"Item 4","label":"","Description":""}
]