typeahead.js远程beforesend发布数据问题

时间:2013-09-18 13:53:30

标签: javascript jquery typeahead.js

我已经把它发布到了typeahead github,但我想也许我可以在这里问一下并找到答案。

我正在尝试使用远程查询作为我们的typeahead数据源。为此,我想创建一个POST并将我的查询参数放在有效负载中。当我在beforeSend函数中将settings.type设置为POST时,它可以工作,但是当我设置settings.data时,我没有看到我的调试器中设置的数据,然后在我的示例中我使用一个简单的http服务返回你的有效载荷,这也验证了数据没有被发送。在typeahead.js下面使用jQuery 1.9.1,特别是使用它的ajax beforeSend调用。据我所知,看看jQuery ajax api我在typeahead beforeSend函数中设置了适当的值。

这是我的代码并在jsfiddle http://jsfiddle.net/75mCa/2/

中运行
$('.autocomplete-es .typeahead').typeahead({
    name: 'es-tags',
    remote: {
        url: 'http://httpbin.org/post',
        beforeSend: function (jqXhr, settings) {

            console.log('type is initially : ' + settings.type);
            console.log('data is initially : ' + settings.data);

            settings.type = 'POST';
            settings.data = { fields: ['tags.tag'], query: { prefix: {  tag: 'emp' } } }
            //settings.contentType = 'application/json; charset=utf-8';

            console.log('type is now : ' + settings.type);
            console.log('data is now : ' + settings.data);

            //settings.processData = false;
            //settings.traditional = true;

            return true;
        },
        filter: function (data) {
            console.log(data.data);
            console.log(data.data.fields[0]);
            //do actual processing...for now just looking for this not to throw an error
        //    return data.hits.hits[0].fields.tags.tag;
        }
    }
});

感谢您的帮助, ģ

1 个答案:

答案 0 :(得分:3)

我有完全相同的用例(将typehead.js与elasticsearch集成),并遇到了同样的问题。问题最终是jquery中的代码行检查你是否有表单数据到POST:

xhr.send( ( s.hasContent && s.data ) || null );

...然后继续在s.hasContent=true中设置beforeSend,一切正常。

相关问题