Typeahead.js remote access to the message

I already posted this on typeahead github, but I thought that maybe I could ask here and find the answer.

I am trying to use a remote query as our data source for typeahead. To do this, I would like to create a POST and put my request parameters in a payload. When I set the settings.type parameter to POST in the beforeSend function, it works, but when I set the settings.data parameters, I don’t see the data set in my debuggers, and then in my example I use a simple http service that returns your useful load, and it also checks that no data is being sent. Under typeahead.js, jQuery 1.9.1 is used and, in particular, using ajax beforeSend call. As far as I can tell about jQuery ajax api, I set the appropriate values ​​in the typeahead beforeSend function.

Here is my code and it works in jsfiddle http://jsfiddle.net/75mCa/2/

$('.autocomplete-es .typeahead').typeahead({
    name: 'es-tags',
    remote: {
        url: 'http://httpbin.org/post',
        beforeSend: function (jqXhr, settings) {

            console.log('type is initially : ' + settings.type);
            console.log('data is initially : ' + settings.data);

            settings.type = 'POST';
            settings.data = { fields: ['tags.tag'], query: { prefix: {  tag: 'emp' } } }
            //settings.contentType = 'application/json; charset=utf-8';

            console.log('type is now : ' + settings.type);
            console.log('data is now : ' + settings.data);

            //settings.processData = false;
            //settings.traditional = true;

            return true;
        },
        filter: function (data) {
            console.log(data.data);
            console.log(data.data.fields[0]);
            //do actual processing...for now just looking for this not to throw an error
        //    return data.hits.hits[0].fields.tags.tag;
        }
    }
});

Thanks for any help, G

+1
1

( typehead.js elasticsearch) . , jquery, , POST:

xhr.send( ( s.hasContent && s.data ) || null );

... s.hasContent=true beforeSend, .

+3

Source: https://habr.com/ru/post/1611844/


All Articles