typeahead bloodhound自定义标题

时间:2015-12-29 02:45:05

标签: ajax typeahead bloodhound

我想在我的血腥请求中添加一个自定义标题,这是我的代码,但它不起作用。

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'
    }
});

有什么想法吗?

1 个答案:

答案 0 :(得分:9)

为typeahead.js v0.11.0添加自定义标头的方法是:

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 });