简体   繁体   English

提前输入问题-Ajax仅返回一次Json数据

[英]Typeahead issue - Ajax only returning Json data once

I've been pulling my hair out for a few hours with Twitter's Typeahead plugin. 我已经使用Twitter的Typeahead插件将头发拉了几个小时。

Basically, we're pulling the data from a web service (an Asmx webservice) with a response format of .JSON. 基本上,我们从响应格式为.JSON的Web服务(Asmx Web服务)中提取数据。

Once we get it via BloodHound, we then convert it into a string, strip out some additional characters before re-converting it into Json and then passing it to Typeahead. 一旦通过BloodHound获得它,我们便将其转换为字符串,去除一些其他字符,然后再将其转换为Json,然后将其传递给Typeahead。

 var engine= new Bloodhound({
        datumTokenizer: function (d) {
            return Bloodhound.tokenizers.whitespace("value")
        },
        queryTokenizer: Bloodhound.tokenizers.whitespace,
        prefetch : {
            ttl : 1000,
            url : '/webservice.asmx'
        },
        remote: {
            ttl : 1000,
            url: '/webservice.asmx',
            ajax: {
                beforeSend: function (jqXhr, settings) {
                    jqXhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8');
                    settings.data = '{"query":"' + $('#inputText').val() + '"}';
                    alert(settings.data);
                },
                type: "POST",
                context: this
            },
            filter: function (response) {
                //return response;
                if (response.hasOwnProperty('d')) {

                    var returned = String(response.d);
                    returned = returned.slice(14,-1);

    var parsedJSON =     jQuery.parseJSON(returned);
                    return  parsedJSON ;
                }
                else {
                    return response;
                }
            }
        }
    });

    engine.initialize();

    $('#inputText').typeahead(null, {
        source: engine.ttAdapter()
    });

When typing into the input field, the code fetches the data based on the query via Ajax ONCE and ONCE ONLY, however it passes it through the filter multiple times when the user types more characters into the input box. 在输入字段中键入代码时,代码将通过Ajax ONCE和ONCE ONLY根据查询获取数据,但是当用户在输入框中键入更多字符时,它将多次通过过滤器。 I believe this is the problem. 我相信这是问题所在。 Because we have to use a type of POST due to the method of using a Asmx webservice, Ajax doesn't seem to be able to make more than one request, no matter how many characters we type into the input box. 由于使用Asmx Web服务的方法导致我们必须使用一种POST类型,因此无论我们在输入框中键入多少个字符,Ajax似乎都无法发出多个请求。

As I say, this has been driving us mad for a few days now so any help would be incredibly appreciated. 正如我所说,这已经使我们发疯了几天,因此,任何帮助都将受到极大的赞赏。

This blog helped solve the answer - http://www.weezey.com/2014/03/bringing-it-all-together/ 该博客有助于解决问题-http: //www.weezey.com/2014/03/bringing-it-all-together/

Looked like we overcomplicated things a bit. 看起来我们有点复杂了。 We still had to strip out and reconvert the Json our webserver was spitting back in the Ajax section, but we got it working. 我们仍然必须删除并重新转换我们的Web服务器在Ajax部分中吐出来的Json,但是我们让它工作了。

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

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