Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typeahead bloodhound custom headers

I would like to add a custom header into my bloodhound request, here is my code but it doesn't work.

var datasource = new Bloodhound({
    datumTokenizer: Bloodhound.tokenizers.whitespace,
    queryTokenizer: Bloodhound.tokenizers.whitespace,
    remote: {
        url: url,
        prepare: function (query, settings) {
            var authHeaders;
            settings.type = "POST";
            settings.data = param + '=' + query;
            settings.headers = {
                'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content')
            };


            return settings;
        },
        wildcard: '%QUERY'
    }
});

Any idea?

like image 567
Johnny Nguyen Avatar asked Dec 29 '15 02:12

Johnny Nguyen


1 Answers

The way to add custom headers for typeahead.js v0.11.0 is:

var bloodhound = new Bloodhound({
datumTokenizer: function (d) {
  return Bloodhound.tokenizers.whitespace(d.value);
},
queryTokenizer: Bloodhound.tokenizers.whitespace,

remote: {
  url: 'https://server/api/v1/clients?auth_token=3000193534a97fc4a252ddfc966e3ef178c071e4079d35ce4b',
  prepare: function (query, settings) {
    settings.url = settings.url + '&q=' + query
    settings.headers = {
      "Authorization": "Basic " + btoa("username:password")
    };

    return settings;
  }
},
limit: 5 });
like image 97
Esteban Gatjens Avatar answered Sep 28 '22 03:09

Esteban Gatjens