我尝试了很多解决方案,但看起来Typeahead只是忽略了 beforeSend 字段。 这是我的代码:
var categories = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('attName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'functions/suggestData.php',
replace: function (url) {
var q = '?start='+$('input:focus').val();
var inputItems = elt.tagsinput('items');
if(inputItems.length > 0)
$.map(inputItems,function(val, i){
//val.attName
});
return url+q+"&rKey="+ Math.random();
},
beforeSend: function (jqXhr, settings){
alert(1);
jqXhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
settings.type = 'POST';
settings.hasContent = true;
settings.data = $.param({beneficiary: "test"});
},
cache: false
}
});
var elt = $('.tagInput');
elt.tagsinput({
itemValue: 'additionalData',
itemText: 'attName',
allowDuplicates:true,
typeaheadjs: [{ minLength: 0},[{
name: 'categories',
displayKey: 'attName',
source: categories,
templates: {
header: queryTemplateHeader
}
}]]
});
现在数据继续发送为" GET "没有帖子数据..( q:" test" ), 我错过了什么?
答案 0 :(得分:1)
假设你在0.11分支上,没有beforeSend,prepare的settings属性是jQuery AJAX设置,所以你的代码看起来应该是这样的:
var categories = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('attName'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'functions/suggestData.php',
replace: function (url) {
},
prepare: function(query, settings) {
var url = settings.url;
var q = '?start='+$('input:focus').val();
var inputItems = elt.tagsinput('items');
if(inputItems.length > 0)
$.map(inputItems,function(val, i){
//val.attName
});
settings.url = url + q + "&rKey=" + Math.random();
settings.contentType = 'application/x-www-form-urlencoded';
settings.type = 'POST';
settings.hasContent = true;
settings.data = $.param({beneficiary: "test"});
return settings;
},
cache: false
}
});
var elt = $('.tagInput');
elt.tagsinput({
itemValue: 'additionalData',
itemText: 'attName',
allowDuplicates:true,
typeaheadjs: [{ minLength: 0},[{
name: 'categories',
displayKey: 'attName',
source: categories,
templates: {
header: queryTemplateHeader
}
}]]
});