JQueryUI的autocomplete调用远程数据(ashx和webservice)

来源:互联网 发布:淘宝托管代运营 编辑:程序博客网 时间:2024/06/03 20:46
 
<html xmlns="http://www.w3.org/1999/xhtml" ><head runat="server">    <title></title>    <link rel="stylesheet" href="../JQueryUI/themes/base/jquery.ui.all.css"><script type="text/javascript" src="../JQuery/jquery-1.6.2.min.js"></script><script type="text/javascript" src="../JQueryUI/ui/jquery.ui.core.js"></script><script type="text/javascript" src="../JQueryUI/ui/jquery.ui.widget.js"></script><script type="text/javascript" src="../JQueryUI/ui/jquery.ui.position.js"></script><script type="text/javascript" src="../JQueryUI/ui/jquery.ui.autocomplete.js"></script><script type="text/javascript" src="../json/json2.js"></script><style type="text/css">#project-label {display: block;font-weight: bold;margin-bottom: 1em;}#project-icon {float: left;height: 32px;width: 32px;}#project-description {margin: 0;padding: 0;}</style><script type="text/javascript">    $(function() {        var projects = [{    value: "jquery",    label: "jQuery",    desc: "the write less, do more, JavaScript library",    icon: "jquery_32x32.png"},{    value: "中国",    label: "jQuery UI",    desc: "the official user interface library for jQuery",    icon: "jqueryui_32x32.png"},{    value: "sizzlejs",    label: "Sizzle JS",    desc: "a pure-JavaScript CSS selector engine",    icon: "sizzlejs_32x32.png"}    ];        $("#project").autocomplete({            minLength: 0,            //source: "GetAllWords.ashx",            source: function(request, response) {                //$.ajax({                //    url: "GetAllWords.ashx",                //    data: request,                //    dataType: "json",                 //    success: function(data) {                //        response(data);                //    },                //    error: function(result, status) {                //        alert('错误信息');                //    }                //});            $.ajax({                    url: "GetAllWordsService.asmx/GetAllWords",                    datatype: "json",                    data: "{ 'term': 'j'}",                    type: "POST",                    contentType: "application/json",                    success: function(data) {                    var dd = eval("(" + data.d + ")")                         response(dd);                    },                    error: function(result, status) {                        alert('错误信息');                    }                });            },            focus: function(event, ui) {                $("#project").val(ui.item.label);                return false;            },            select: function(event, ui) {                $("#project").val(ui.item.label);                $("#project-id").val(ui.item.value);                $("#project-description").html(ui.item.desc);                $("#project-icon").attr("src", "images/" + ui.item.icon);                return false;            }        })    .data("autocomplete")._renderItem = function(ul, item) {        return $("<li></li>")    .data("item.autocomplete", item)    .append("<a>" + item.label + "<br>" + item.desc + "</a>")    .appendTo(ul);    };        $("#btnSubmit").click(function() {            var id = $("#project-id").val();            alert(id);        });    });</script></head><body>    <form id="form1" runat="server">    <div class="demo">    <div id="project-label">Select a project (type "j" for a start):</div>    <img id="project-icon" src="images/transparent_1x1.png" class="ui-state-default"/>    <input id="project"/>    <input type="hidden" id="project-id"/><input type="button" id="btnSubmit" title="提交" value="提交"/>    <p id="project-description"></p>    </div><!-- End demo -->    <div class="demo-description">    <p>You can use your own custom data formats and displays by simply overriding the default focus and select actions.</p>    <p>Try typing "j" to get a list of projects or just press the down arrow.</p>    </div><!-- End demo-description -->    </form></body></html>

 

后台代码:

GetAllWords.ashx

Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest        If context.Request.QueryString("term") IsNot Nothing AndAlso context.Request.QueryString("term") <> "" Then            context.Response.ContentType = "text/plain"            context.Response.Buffer = True            context.Response.AddHeader("pragma", "no-cache")            context.Response.AddHeader("cache-control", "")            context.Response.CacheControl = "no-cache"            Dim strdata As String = ""            strdata = "[{""value"": ""jquery"",""label"": ""jQuery"",""desc"": ""the write less, do more, JavaScript library"",""icon"": ""jquery_32x32.png""},"            strdata += "{""value"": ""china"",""label"": ""jQuery UI"",""desc"": ""the official user interface library for jQuery"",""icon"": ""jqueryui_32x32.png""},"            strdata += "{""value"": ""sizzlejs"",""label"": ""Sizzle JS"",""desc"": ""a pure-JavaScript CSS selector engine"",""icon"": ""sizzlejs_32x32.png""}]"            context.Response.Write(strdata)            context.Response.Flush()            context.Response.Close()            context.Response.[End]()        End If
    End Sub

 

GetAllWordsService.asmx

Imports System.Web.ServicesImports System.Web.Services.ProtocolsImports System.ComponentModel' 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。<System.Web.Script.Services.ScriptService()> _<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _<ToolboxItem(False)> _Public Class GetAllWordsService    Inherits System.Web.Services.WebService    <WebMethod()> _    Public Function HelloWorld() As String        Return "Hello World"    End Function    <WebMethod(EnableSession:=True)> _    Public Function GetAllWords(ByVal term As String) As String        Dim strdata As String = ""        strdata = "[{""value"": ""jquery"",""label"": ""jQuery"",""desc"": ""the write less, do more, JavaScript library"",""icon"": ""jquery_32x32.png""},"        strdata += "{""value"": ""china"",""label"": ""jQuery UI"",""desc"": ""the official user interface library for jQuery"",""icon"": ""jqueryui_32x32.png""},"        strdata += "{""value"": ""sizzlejs"",""label"": ""Sizzle JS"",""desc"": ""a pure-JavaScript CSS selector engine"",""icon"": ""sizzlejs_32x32.png""}]"        Return strdata    End FunctionEnd Class


 

 

 

 


 

原创粉丝点击