预先输入猎犬的POST请求(Typeahead Bloodhound POST request)

87 IT屋

I cannot seem to get a remote query to use POST properly.

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

the queryInput.val() does not get the current value of the object only the value at the time bloodhound object is instantiated. How can I get the query string into the ajax data options?

解决方案

You can use beforeSend of $.ajax

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"

        }
    }
});

我似乎无法获得远程查询以正确使用POST。



  var债权人=新猎犬({
datumTokenizer:函数(d){
return Bloodhound.tokenizers.whitespace(d.value)
},
queryTokenizer:Bloodhound.tokenizers.whitespace,
remote:{
url: ../getCreditors,
替换:function(url,query){
return url +# + query;
},
ajax:{
类型: POST,
数据:$。param({q:queryInput.val()})
}
}
});


queryInput.val()不获取对象的当前值,仅获取当时的值猎犬对象被实例化。如何将查询字符串放入ajax数据选项中?


解决方案

您可以使用$ .ajax的beforeSend发送



  var债权人=新的Bloodhound({
datumTokenizer:函数(d){
return Bloodhound.tokenizers.whitespace(d.value)
},
queryTokenizer:Bloodhound.tokenizers.whitespace,
远程:{
url: ../getCreditors,

replace:function(url,query){
return url +# + query;
},
ajax:{
beforeSend:function(jqXhr,settings){
settings.data = $ .param({q:queryInput .val()})
},
类型: POST

}
}
});

本文地址:IT屋 » 预先输入猎犬的POST请求