2

I'm looking to add an additional parameter to a jquery UI auto complete request without having to nest the json return in an ajax call. I would envision something like the following working, however the data: option is not passed over to the ajax request like it is on a normal jquery ajax request.

 $("#div").autocomplete({
        source: 'ajax.php',
        minLength: 2,
        data: '&action=getUserName',
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

tl;dr I need help passing &action=getUserName over to the ajax for my autocomplete, preferably without nesting it in an ajax callback.

aaronasterling
  • 68,820
  • 20
  • 127
  • 125
AceoStar
  • 431
  • 4
  • 7

1 Answers1

1

Try this instead:

$("#div").autocomplete({
        source: 'ajax.php',

        extraParams: {
            action: function() {
                  return "getUserName";
            }
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.value + " aka " + ui.item.id :
                "Nothing selected, input was " + this.value );
        }
});

per docs: http://docs.jquery.com/Plugins/Autocomplete/autocomplete#url_or_dataoptions

edit: added correction based on jquery autocomplete extraParams

Community
  • 1
  • 1
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
  • Ah, it's `extraParams`. Updated answer – Chris Baker Feb 11 '11 at 19:14
  • Hmm, I see this in the documentation, and I'm using this exact code and the param still isn't being passed. I'm print_r($_REQUEST) and all I'm getting is the term default. Thanks for the help so far. – AceoStar Feb 11 '11 at 19:34
  • Updated answer based on: http://stackoverflow.com/questions/1666344/jquery-autocomplete-extraparams - I had it wrong. Try my new code. – Chris Baker Feb 11 '11 at 19:57
  • The docs don't specify that this correction is in fact the answer, but that other stack overflow question indicates the OP had luck. Let me know.... I would have thought that what we had to begin with would have worked :| – Chris Baker Feb 11 '11 at 20:01
  • Still no luck. As a bit of a reference, this code is being called from a function that is being triggered on keyUp. – AceoStar Feb 11 '11 at 20:30
  • 4
    FYI this answer is wrong. jQuery autocomplete is NOT the same as jQueryUI autocomplete. – Bot Apr 18 '11 at 20:06