我似乎无法获得正确使用POST的远程查询。
var creditors = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value)
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "../getCreditors",
replace: function(url, query) {
return url + "#" + query;
},
ajax : {
type: "POST",
data: $.param({q: queryInput.val()})
}
}
});
queryInput.val()只获取对象的当前值,而不是实例化bloodhound对象时的值。如何将查询字符串放入ajax数据选项中?
答案 0 :(得分:16)
您可以使用$ .ajax的beforeSend
var creditors = new Bloodhound({
datumTokenizer: function (d) {
return Bloodhound.tokenizers.whitespace(d.value)
},
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: "../getCreditors",
replace: function(url, query) {
return url + "#" + query;
},
ajax : {
beforeSend: function(jqXhr, settings){
settings.data = $.param({q: queryInput.val()})
},
type: "POST"
}
}
});
答案 1 :(得分:11)
You can use the prepare
property with remote
or prefetch
, mind that the function signature changes.
An example with prefetch
:
var Bloodhound = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.whitespace,
queryTokenizer: Bloodhound.tokenizers.whitespace,
prefetch: {
url: remote,
prepare: function (settings) {
settings.type = "POST";
settings.contentType = "application/json; charset=UTF-8";
return settings;
},
remote: function (query, settings) {
settings.type = "POST";
settings.data = {q: query, foo: 'bar'}; // you can pass some data if you need to
return settings;
}
}
});
Remember that with remote
the function signature changes with function(query, settings)
.
For reference: github.com/twitter/typeahead.js/issues/1236
答案 2 :(得分:0)
我发现上面提到的ajax'beforeSend'方法是最好的。
尽管如此,改变网址也很重要。否则Typeahead没有打扰另一个请求。所以我只是在网址的末尾添加了一个虚假参数。喜欢这个
这样当打包的ajax数据发生变化时,我确信向服务器发出了新的请求。