无法让 jQuery AutoComplete 与外部 JSON 配合使用
我正在开发一个 ASP.NET 应用程序,需要 jQuery AutoComplete。目前,当我在 txt63 输入框中输入数据时,没有任何反应(在你因为我使用像 txt63 这样的名称而责备我之前,我知道,我知道......但这不是我的决定:D) 。
这是我的 javascript 代码
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.1.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var theSource = '../RegionsAutoComplete.axd?PID=<%= hidden62.value %>'
$(function() {
$('#<%= txt63.ClientID %>').autocomplete({
source: theSource,
minLength: 2,
select: function(event, ui) {
$('#<%= hidden63.ClientID %>').val(ui.item.id);
}
});
});
,这是我的 HTTP 处理程序
Namespace BT.Handlers
Public Class RegionsAutoComplete : Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
''# the page contenttype is plain text'
context.Response.ContentType = "application/json"
context.Response.ContentEncoding = Encoding.UTF8
''# set page caching'
context.Response.Cache.SetExpires(DateTime.Now.AddHours(24))
context.Response.Cache.SetCacheability(HttpCacheability.Public)
context.Response.Cache.SetSlidingExpiration(True)
context.Response.Cache.VaryByParams("PID") = True
Try
''# use the RegionsDataContext'
Using RegionDC As New DAL.RegionsDataContext
''# query the database based on the querysting PID'
Dim q = (From r In RegionDC.bt_Regions _
Where r.PID = context.Request.QueryString("PID") _
Select r.Region, r.ID)
''# now we loop through the array'
''# and write out the ressults'
Dim sb As New StringBuilder
sb.Append("{")
For Each item In q
sb.Append("""" & item.Region & """: """ & item.ID & """,")
Next
sb.Append("}")
context.Response.Write(sb.ToString)
End Using
Catch ex As Exception
HealthMonitor.Log(ex, False, "This error occurred while populating the autocomplete handler")
End Try
End Sub
End Class
End Namespace
我的 ASPX 页面的其余部分具有适当的控件,因为我使用旧版本的 jQuery 库来进行此操作。我正在尝试让它与新的 CDN 一起工作,因为我听说“dev”CDN 即将过时。
任何帮助或指导将不胜感激。
I'm working on an ASP.NET app where I'm in need of jQuery AutoComplete. Currently there is nothing happening when I type data into the txt63 input box (and before you flame me for using a name like txt63, I know, I know... but it's not my call :D ).
Here's my javascript code
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.1.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var theSource = '../RegionsAutoComplete.axd?PID=<%= hidden62.value %>'
$(function() {
$('#<%= txt63.ClientID %>').autocomplete({
source: theSource,
minLength: 2,
select: function(event, ui) {
$('#<%= hidden63.ClientID %>').val(ui.item.id);
}
});
});
and here is my HTTP Handler
Namespace BT.Handlers
Public Class RegionsAutoComplete : Implements IHttpHandler
Public ReadOnly Property IsReusable() As Boolean Implements System.Web.IHttpHandler.IsReusable
Get
Return False
End Get
End Property
Public Sub ProcessRequest(ByVal context As System.Web.HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
''# the page contenttype is plain text'
context.Response.ContentType = "application/json"
context.Response.ContentEncoding = Encoding.UTF8
''# set page caching'
context.Response.Cache.SetExpires(DateTime.Now.AddHours(24))
context.Response.Cache.SetCacheability(HttpCacheability.Public)
context.Response.Cache.SetSlidingExpiration(True)
context.Response.Cache.VaryByParams("PID") = True
Try
''# use the RegionsDataContext'
Using RegionDC As New DAL.RegionsDataContext
''# query the database based on the querysting PID'
Dim q = (From r In RegionDC.bt_Regions _
Where r.PID = context.Request.QueryString("PID") _
Select r.Region, r.ID)
''# now we loop through the array'
''# and write out the ressults'
Dim sb As New StringBuilder
sb.Append("{")
For Each item In q
sb.Append("""" & item.Region & """: """ & item.ID & """,")
Next
sb.Append("}")
context.Response.Write(sb.ToString)
End Using
Catch ex As Exception
HealthMonitor.Log(ex, False, "This error occurred while populating the autocomplete handler")
End Try
End Sub
End Class
End Namespace
The rest of my ASPX page has the appropriate controls as I had this working with the old version of the jQuery library. I'm trying to get it working with the new one because I heard that the "dev" CDN was going to be obsolete.
Any help or direction will be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,经过几个小时的努力,我的 iHttpHandler 很好地传递了数据。如果您觉得这可以做得更好,请随时发表评论
Well, after a bunch of hours working on this, I got my iHttpHandler delivering data fairly nicely. Feel free to comment if you feel this could be better