简体   繁体   中英

jQuery UI Autocomplete: How can I pass additional data with my Ajax call?

I have two jQuery UI Autocomplete widgets set up. With the first autocomplete, the user will enter a client's name - the autocomplete will narrow it down until the correct client has been chosen. I then have a callback that takes the ID of the client returned and puts it into a hidden input field.

Next, is a second autocomplete field. When searched in, this needs to send two variables to the server - the user's search string ( term ), and the User ID of the client that was searched for previously.

I have no problems dealing with the server side of things, but where I'm struggling is how to pass 2 variables to the Ajax call, rather than just term . In my PHP backend, I need to query against the User ID as well, to only return properties belonging to that user.

How can I do this?

Thanks!

Edit: Thanks to @JohnP, this was what I ended up - seems to work fine for me. Posting this here for reference for anyone in future who drops by:

source: function (request, response) {

    var request_data = {
        term: request.term,
        client_id: $('input#client_id_string').val()
    };

    var url = 'http://mysite.com/search/ajax_search';

    $.getJSON(url, request_data, function (data, status, xhr) {
         response(data);
    });
},

You can override the source method if it's a static method.

//code
source: function (request, response) {
    var term = request.term;
     //caching if yo uwant
     var myCustomVar = 42;

     $.getJSON(url + term + '/' + myCustomVar , request, function (data, status, xhr) {
          response(data);
     });
},
//code

You can either make it part of the URL or just pass it along with the request, if you want.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM