jQuery 预先输入 Bloodhound POST 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21821658/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:42:29  来源:igfitidea点击:

Typeahead Bloodhound POST request

jquerytypeaheadtypeahead.jstwitter-typeahead

提问by Ablue

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

我似乎无法通过远程查询正确使用 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()})
        }
    }
});

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?

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

采纳答案by holylaw

You can use beforeSend of $.ajax

您可以使用 beforeSend 的 $.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"

        }
    }
});

回答by Atropo

You can use the prepareproperty with remoteor prefetch, mind that the function signature changes. An example with prefetch:

您可以将prepare属性与remote或 一起使用prefetch,注意函数签名会发生变化。一个例子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 remotethe function signature changes with function(query, settings).

请记住,remote函数签名随function(query, settings).

For reference: github.com/twitter/typeahead.js/issues/1236

供参考:github.com/twitter/typeahead.js/issues/1236

回答by Jamie Popkin

I found the ajax 'beforeSend' method holylaw mentioned worked the best.

我发现 Holylaw 提到的 ajax 'beforeSend' 方法效果最好。

It was important to alter the url as well though. Otherwise Typeahead didn't bother making another request. So I just added a bogus parameter at the end of the url. Like this

不过,更改 url 也很重要。否则 Typeahead 不会费心提出另一个请求。所以我只是在 url 的末尾添加了一个虚假参数。像这样

http://mylittleservice.com?blah=%QUERY

http://mylittleservice.com?blah=%QUERY

That way when the ajax data packaged changed I was assured a fresh request to the server.

这样,当打包的 ajax 数据发生变化时,我就可以向服务器发出新的请求。