Search code examples
javascriptajaxautocompletetypeaheadbloodhound

Typeahead: Setting HTTP GET headers for the Bloodhound remote option?


So, I am working on an AutoComplete module for a website to which i use Typeahead.js and bloodhound.js. Basically, the AutoComplete module must be able to fetch data from a remote URL which then returns a response in JSON format.

To this I have a couple of questions which the official documentation seems to say very little about :/

Reading what I believe is the official Bloodhound documentation, I can see that there is a remote option which I very clearly have to use for this. However, the options for the remote property does not seem to give me the ability to provide authetication headers to the HTTP GET message. The data I have to fetch requires an API-Key to be included as a HTTP header. So, the first question is:

How do I go about adding an API-Key to a Bloodhound remote request?

Secondly, am I right in understanding that I have to use the transform property to provide a function which can take the JSON array of objects and turn it into the suggestions that I want listed in my typeahead?

Thank you :)


Solution

  • So, I found this out myself. It's very simple, really, though the documentation doesn't seem to clear about it.

    The prepare function takes the settings object as an argument. This settings object seems to be a jquery ajax options object. At least it worked when I tried to apply the standard parameters from the jQuery documentation :)