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

后端 未结 3 910
攒了一身酷
攒了一身酷 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

提交回复
热议问题