typeahead bloodhound custom headers
Asked Answered
G

1

8

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?

Gumm answered 29/12, 2015 at 2:45 Comment(3)
Could you find a solution for this?Hayrack
@KunalDethe take a look at my accepted down vote with no reason answerGumm
https://mcmap.net/q/744620/-twitter-39-s-typeahead-bloodhound-what-is-the-equivalent-of-quot-query-quot-when-using-ajax-data-and-postLulululuabourg
P
13

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 });
Pooka answered 22/12, 2016 at 20:52 Comment(1)
Your answer was perfect. I spent way too much time trying to solve something that should have been simple. ThanksStubblefield

© 2022 - 2024 — McMap. All rights reserved.