简体   繁体   English

typeahead.js远程发送数据前问题

[英]typeahead.js remote beforesend post data issue

I'd already posted this to the typeahead github, but I thought perhaps I could ask here and find an answer. 我已经把它发布到了预先输入的 github上,但是我想也许我可以在这里问一下并找到答案。

I'm trying to use a remote query as our of our datasources for typeahead. 我正在尝试使用远程查询作为我们的数据源来进行预输入。 To do so I would want to create a POST and put my query parameters in the payload. 为此,我想创建一个POST并将查询参数放入有效负载中。 When I set the settings.type to POST in the beforeSend function it works, but when I set the settings.data I am not seeing the data being set in my debuggers, and then in my example I use a simple http service which returns your payload, and this also verifies the data is not being sent. 当我在beforeSend函数中将settings.type设置为POST时,它可以工作,但是当我设置settings.data时,我看不到调试器中正在设置的数据,然后在我的示例中,我使用了一个简单的http服务,该服务返回了有效负载,这也可以验证数据是否没有发送。 Underneath typeahead.js is using jQuery 1.9.1 and in particular using it's ajax beforeSend call. 在typeahead.js下面使用的是jQuery 1.9.1,尤其是使用ajax beforeSend调用。 As far as I can tell looking at the jQuery ajax api I am setting the appropriate values in the typeahead beforeSend function. 据我所知,我正在jQuery ajax api中设置typeahead beforeSend函数中的适当值。

Here is my code and running in jsfiddle http://jsfiddle.net/75mCa/2/ 这是我的代码,并在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;
        }
    }
});

Thanks for any help, G 感谢您的帮助,G

I have this exact same use case (integrate typehead.js with elasticsearch), and ran into the same problem. 我有这个完全相同的用例(将typehead.js与elasticsearch集成在一起),并遇到了相同的问题。 The problem ends up being that the line of code in jquery that is checking whether or not you have form data to POST is: 问题最终是jquery中用于检查是否有要发送的表单数据的代码行是:

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

... and so just go ahead and set s.hasContent=true in beforeSend and everything works. ......所以先走一步,设置s.hasContent=truebeforeSend和一切正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM