Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 同时独立的AJAX请求_Javascript_Jquery_Html - Fatal编程技术网

Javascript 同时独立的AJAX请求

Javascript 同时独立的AJAX请求,javascript,jquery,html,Javascript,Jquery,Html,我有一个搜索建议脚本,它从两个Google API中提取结果,按整数值排序结果,然后将结果显示给用户。该脚本确保根据每个查询的rel属性,只返回与每个查询最相关的五个响应 JSFiddle: 但是,在当前设置下,没有容错能力;例如,一个API可用或超过一个API的查询限制,因此不会返回任何结果 如何解决这个问题 我的jQuery代码是: var combined=[]; $(document).ready(function(){ $("#search").keyup(function(e

我有一个搜索建议脚本,它从两个Google API中提取结果,按整数值排序结果,然后将结果显示给用户。该脚本确保根据每个查询的
rel
属性,只返回与每个查询最相关的五个响应

JSFiddle:

但是,在当前设置下,没有容错能力;例如,一个API可用或超过一个API的查询限制,因此不会返回任何结果

如何解决这个问题

我的jQuery代码是:

var combined=[];
$(document).ready(function(){
    $("#search").keyup(function(e){
        $(this).html("");
        $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?",function(data1){
            $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?",function(data2){
                for(var key in data1[1]){
                    if(data1[4]["google:suggesttype"][key]=="NAVIGATION"){
                        combined.push("<li rel='"+data1[4]["google:suggestrelevance"][key]+"'><a href='"+data1[1][key]+"'>"+data1[2][key]+"</a></li>");
                    }else{
                        combined.push("<li rel='"+data1[4]["google:suggestrelevance"][key]+"'>"+data1[1][key]+"</li>");
                    }
                }
                for(var key in data2.result){
                    combined.push("<li rel='"+Math.round(data2.result[key].score*5)+"'> Freebase: "+data2.result[key].name+"</li>");
                }
                combined.sort(function(a,b){
                    return +$(b).attr("rel")-+$(a).attr("rel");
                });
                $("#suggest").html(combined.slice(0,5).join(""));
                combined=[];
            });
        });
    });
});
var组合=[];
$(文档).ready(函数(){
$(“#搜索”).keyup(函数(e){
$(this.html(“”);
$.getJSON(“http://suggestqueries.google.com/complete/search?q=“+$(“#搜索”).val()+”&client=chrome&callback=?”,函数(data1){
$.getJSON(“https://www.googleapis.com/freebase/v1/search?query=“+$(“#搜索”).val()+”&limit=3&encode=html&callback=?”,函数(数据2){
for(数据1[1]中的var键){
如果(数据1[4][“google:suggesttype”][key]==“导航”){
组合.push(“
  • ”); }否则{ 组合.push(“
  • ”+data1[key]+“
  • ”; } } for(data2.result中的var键){ 组合.push(“
  • Freebase:“+data2.result[key].name+”
  • ”; } 组合排序(函数(a,b){ return+$(b).attr(“rel”)-+$(a).attr(“rel”); }); $(“#建议”).html(组合的.slice(0,5).join(“”); 合并=[]; }); }); }); });
    我已经更新了你的小提琴:

    当出现故障时,我突然启动了<代码>$,它将在完成或失败的情况下始终返回承诺。传入一个延迟列表,就会得到一个结果列表!这不是一个完整的解决方案,但我希望这是朝着正确方向迈出的好一步

    $.whenFaultTolerant = function(things) {
        var remaining = Object.keys(things).length;
        var outputs = {};
        var dfd = $.Deferred();
        $.each(things, function(key, thing) {
            thing.always(function(data) {
                outputs[key] = data;
                --remaining || dfd.resolve(outputs);
            });
        });
        return dfd.promise();
    };
    
    用法:

        $.whenFaultTolerant({
            suggestqueries: $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?"),
            googleapis: $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?")
        }).done(function(data){
            console.log(data);
        });
    
    产出:

    {
        "suggestqueries": <the results from suggestqueries.google.com>,
        "googleapis": <the results from www.googleapis.com>
    }
    
    {
    “建议查询”:,
    “googleapis”:
    }
    
    这是否有帮助:@JoeFrambach并非如此。我需要分离现有的AJAX请求,但要确保它们同时被编译和排序。有什么想法吗?