开发者

Jquery Autocomplete from an Array in Session

开发者 https://www.devze.com 2023-02-18 15:13 出处:网络
I will be storing an array in session that will contain data entered by the user (id numbers used to search in several different areas of the application)

I will be storing an array in session that will contain data entered by the user (id numbers used to search in several different areas of the application)

I then want to return the values in this array that is residing in session as auto complete selections.

Getting the array into session and updating it is the easy part, getting it from session into the autocomplete isn't as much so.

I'd rather use the array to do the auto complete instead of converting to json.

I've tried using '<%= SESSION("MEMBER_SEARCH_ARRAY") %>' as the source for the autocomplete but no dice.

Is it possible to do something like:

$.ajax({
     url: "../ajax/MemberAuto.ashx",
     dataType: "text",
     success: function(data) { 
          $('#txtDealerNumber').autoComplete({ source: data }); // <- Object doesn't support this property or method error
     },
          error: function(xhr, status, error) { }
});

when MemberAuto.ashx contains:

Public Class MemberAuto : Implements IHttpHandler, IRequiresSessionState, IReadOnlySessionState

   开发者_StackOverflow社区 Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        context.Response.ContentType = "text/plain"
        context.Response.Write(Join(context.Session("MEMBER_SEARCH_ARRAY"), ","))
        context.Response.End()
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
End Class

unfortunately I get an error following the ajax call in the success.

Any input would be greatly appreciated


Assuming that you are using jQueryUI(seems like autocomplete is not a part of jQuery core), This can be easily implemented in this way. No need of ajax as autocomplete automatically does this.

 $(document).ready(function () {
        $("#txtDealerNumber").autocomplete({
            source: '../ajax/MemberAuto.ashx?AutoComplete'
        }); //function name is autocomplete not autoComplete
 });

where the url ../ajax/MemberAuto.ashx?AutoComplete returns(test it in the browser) in the format

["option 1", "option 2", "option 3"]

see here for more details. http://jqueryui.com/demos/autocomplete/#option-source

Edit1: If you really want to stick in your way. then you need to convert your string to array which can be done using eval().

ie.

$.ajax({
     url: "../ajax/MemberAuto.ashx",
     dataType: "text",
     success: function(data) { 
          $('#txtDealerNumber').autocomplete({ source: eval(data) }); 
          // data should be in the format ["option1", "option2"]
     },
          error: function(xhr, status, error) { }
});

for testing eval http://jsfiddle.net/wZkjZ/


When I want to achieve something like this, I always make my script (in your case memberauto.ashx) print javascript. In PHP for example, I use print('var test = $test');. Afterwards I use eval() to make javascript try to execute whatever the output of the page is. You can then use your new javascript variables / functions / whatever you've made in your ashx in your javascript. Plus, errors are easily catched when you wrap your eval() in try-catch.

0

精彩评论

暂无评论...
验证码 换一张
取 消