2

I'm newbie at all in jQuery and JSON, and spent hours but it still not solved the problem

JSON from server is valid, checked through jsonlit.com but it still show all data (not filtered).

JSON from serverURI.asp

["A. ASRUNADI", "A. MURSYID", "A. RIFANI", "A.Z MAKMUR IS", "ABBAS", "ABDI  IRWANTO"]

my jquery

$("#keyword").autocomplete({
    source: function(request, response){
       $.getJSON("serverURI.asp", function(data){
           var source = data
           response(source);
       });
    }
});

but.... when i put JSON as var in jquery its works... meanwhile i already use utf-8 in my html meta tag

$(function() {
    var availableTags = ["A. ASRUNADI", "A. MURSYID", "A. RIFANI", "A.Z MAKMUR IS", "ABBAS", "ABDI  IRWANTO"];
    $("#keyword").autocomplete({
        source: availableTags
    });
});

my ASP (classic) to generate JSON as follow

    dim strResultEMP
strResultEMP = "["
for strEmpCount = 0  to strTotalCountEmp
    strEmpObj = split(strEmpSplit(strEmpCount), "$$$")
        if strEmpCount < strTotalCountEmp then
            strResultEMP = strResultEMP & """" & ucase(strEmpObj(1)) & """" & ", "
        else
        strResultEMP = strResultEMP & """" & ucase(strEmpObj(1)) & """" & ""
        end if

next
strResultEMP = strResultEMP & "]" 
response.write strResultEMP

FYI, I use JSON2.asp and JSON UTIL, but it still same. to debug and catch the server response I use Firebug.

3
  • 2
    When you use a server-side resource, your server-side code is responsible for doing the filtering. If you want the widget to do the filtering, do the AJAX request to get the source, and then initialize the widget with it. Commented Dec 19, 2012 at 15:18
  • If shows all the tags the problem should be on the filter logic on serverURI.asp Commented Dec 19, 2012 at 15:18
  • @andrew i plan to use widget do filtering. would you pls give me a sample AJAX request for .autocomplete pls Commented Dec 19, 2012 at 15:31

1 Answer 1

6

If you don't want to do the filtering server-side, you should make the AJAX request for the source, then pass that source to the autocomplete widget:

$(function () {
    /* Make the AJAX request once for the source array: */
    $.getJSON("serverURI.asp", function (data) {
        /* Initialize the widget with the data we got back: */
        $("#keyword").autocomplete({
            source: data
        });
    });
});

Keep in mind that if you have a huge amount of data, this can slow down the user's browser. If you have many (>500) items, I would highly recommend doing the filtering on the server.

Sign up to request clarification or add additional context in comments.

3 Comments

OMG... @andrew YOU SAFE MY LIFE... thank you! What's make it difference (my 'fail' code and your code)? seemingly the position of ajax vs .autocomplete?
@akauts: No problem! The difference is that I'm making the server request, then using the results of that request as the source to the autocomplete widget, which is initialized after the AJAX call is completed.
Thank You, i'm now understood. the array must be 'ready' before it called by the widget, right. I'm trying to migrate my mind (my old huge classic-application) because i used with it, to be more interactive and lighter for users. where was i am so long :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.