how to change jQuery autocomplete plugin default querystring key? (term to that i want)

后端 未结 3 887
攒了一身酷
攒了一身酷 2021-01-01 10:42

jQuery autocomplete plugin sent request like this

mysite.com/suggestion?term=Sadegh

is there any way to change term querystring key to anot

相关标签:
3条回答
  • 2021-01-01 11:25

    Actually I dug up the code that the plugin uses. Adapting it to change the term would be something like this:

    $('#form').autocomplete({
        source: (function() {
            var xhr;
            return function(request, response) {
                if (xhr) {
                    xhr.abort();
                }
                xhr = $.ajax({
                    url: 'mysite.com/suggestion',
                    data: {
                        foo: request.term
                    },
                    dataType: 'json',
                    success: function(data) {
                        response(data);
                    },
                    error: function() {
                        response([]);
                    }
                });
            }
        })()
    });
    

    I would say this has 2 advantages:

    1. Abort pending requests
    2. Call the response with an empty set in case of error, which seems more polite to me
    0 讨论(0)
  • 2021-01-01 11:30

    The above is not correct for the current release (1.8.6). Not sure if it ever was...

    $(...).autocomplete({
      source: function(request, response) {
        $.getJSON("url", { foo: request.term }, response);
      }
    });
    

    foo: being the new param key name..

    0 讨论(0)
  • 2021-01-01 11:47

    I assume you're using jQuery UI AutoComplete

    You need to provide a callback as the source, like this:

    $(...).autocomplete({
        source: function(term, callback) {
            $.getJSON("url", { foo: term }, callback);
        }
    });
    
    0 讨论(0)
提交回复
热议问题