typeahead.js远程发送数据前问题 [英] typeahead.js remote beforesend post data issue

查看:67
本文介绍了typeahead.js远程发送数据前问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将其发布到 typeahead github,但是我想也许我可以在这里问一个答案.

I'd already posted this to the typeahead github, but I thought perhaps I could ask here and find an answer.

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

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. 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. Underneath typeahead.js is using jQuery 1.9.1 and in particular using it's ajax beforeSend call. As far as I can tell looking at the jQuery ajax api I am setting the appropriate values in the typeahead beforeSend function.

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

Here is my code and running in 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;
        }
    }
});

感谢您的帮助, G

推荐答案

我有这个完全相同的用例(将typehead.js与elasticsearch集成在一起),并遇到了相同的问题.问题最终是jquery中检查您是否有要发送的表单数据的代码行是:

I have this exact same use case (integrate typehead.js with elasticsearch), and ran into the same problem. 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:

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

...,因此继续进行操作,并在beforeSend中设置s.hasContent=true,一切正常.

... and so just go ahead and set s.hasContent=true in beforeSend and everything works.

这篇关于typeahead.js远程发送数据前问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆