Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.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 使用jQuery同时进行AJAX请求_Javascript_Jquery_Html - Fatal编程技术网

Javascript 使用jQuery同时进行AJAX请求

Javascript 使用jQuery同时进行AJAX请求,javascript,jquery,html,Javascript,Jquery,Html,我有一个搜索建议脚本,它从两个Google API中提取结果,按整数值排序结果,然后将结果显示给用户 但是,在用户按下enter键或return键之前,当前脚本似乎不会从第二个API返回结果。为什么会这样 JSFiddle: 我的代码是: var combined = []; $(document).ready(function(){ $("#search").keyup(function(){ $("#suggest").html(""); $.getJ

我有一个搜索建议脚本,它从两个Google API中提取结果,按整数值排序结果,然后将结果显示给用户

但是,在用户按下enter键或return键之前,当前脚本似乎不会从第二个API返回结果。为什么会这样

JSFiddle:

我的代码是:

var combined = [];
$(document).ready(function(){
    $("#search").keyup(function(){
        $("#suggest").html("");
        $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?",function(data){
            for(var key in data[1]){
                if(data[4]["google:suggesttype"][key]=="NAVIGATION"){
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'><a href='"+data[1][key]+"'>"+data[2][key]+"</a></li>");
                }else{
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'>"+data[1][key]+"</li>");
                }
            }
        });
        $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?",function(data){
            for(var key in data.result){
                combined.push("<li rel='"+Math.round(data.result[key].score*5)+"'> Freebase: "+data.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(函数(){
$(“#建议”).html(“”);
$.getJSON(“http://suggestqueries.google.com/complete/search?q=“+$(“#搜索”).val()+”&client=chrome&callback=?”,函数(数据){
用于(var键入数据[1]){
如果(数据[4][“google:suggesttype”][键]=“导航”){
组合.push(“
  • ”); }否则{ 组合。推送(“
  • ”+data[1][key]+“
  • ”; } } }); $.getJSON(“https://www.googleapis.com/freebase/v1/search?query=“+$(“#搜索”).val()+”&limit=3&encode=html&callback=?”,函数(数据){ for(数据中的var键。结果){ 组合式.push(“
  • Freebase:“+data.result[key].name+”
  • ”; } }); 组合排序(函数(a,b){ return+$(b).attr(“rel”)-+$(a).attr(“rel”); }); $(“#建议”).html(组合的.slice(0,5).join(“”); 合并=[]; }); });
    您可以这样做

    var allData = []
    $.getJSON("/values/1", function(data) {
        allData.push(data);
        if(data.length == 2){
          processData(allData) // where process data processes all the data
        }
    });
    
    $.getJSON("/values/2", function(data) {
        allData.push(data);
        if(data.length == 2){
            processData(allData) // where process data processes all the data
        }
    });
    
    var processData = function(data){
         var sum = data[0] + data[1]
         $('#mynode').html(sum);
    }
    

    你可以这样做

    var allData = []
    $.getJSON("/values/1", function(data) {
        allData.push(data);
        if(data.length == 2){
          processData(allData) // where process data processes all the data
        }
    });
    
    $.getJSON("/values/2", function(data) {
        allData.push(data);
        if(data.length == 2){
            processData(allData) // where process data processes all the data
        }
    });
    
    var processData = function(data){
         var sum = data[0] + data[1]
         $('#mynode').html(sum);
    }
    

    首先将secong getJSON打包,如下所示。代码不错

    var combined = [];
    $(document).ready(function(){
    $("#search").keyup(function(){
        $("#suggest").html("");
        $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?",function(data){
            for(var key in data[1]){
                if(data[4]["google:suggesttype"][key]=="NAVIGATION"){
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'><a href='"+data[1][key]+"'>"+data[2][key]+"</a></li>");
                }else{
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'>"+data[1][key]+"</li>");
                }
            }
    
                    $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?",function(data){
            for(var key in data.result){
                combined.push("<li rel='"+Math.round(data.result[key].score*5)+"'> Freebase: "+data.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(函数(){
    $(“#建议”).html(“”);
    $.getJSON(“http://suggestqueries.google.com/complete/search?q=“+$(“#搜索”).val()+”&client=chrome&callback=?”,函数(数据){
    用于(var键入数据[1]){
    如果(数据[4][“google:suggesttype”][键]=“导航”){
    组合.push(“
  • ”); }否则{ 组合。推送(“
  • ”+data[1][key]+“
  • ”; } } $.getJSON(“https://www.googleapis.com/freebase/v1/search?query=“+$(“#搜索”).val()+”&limit=3&encode=html&callback=?”,函数(数据){ for(数据中的var键。结果){ 组合式.push(“
  • Freebase:“+data.result[key].name+”
  • ”; } }); }); 组合排序(函数(a,b){ return+$(b).attr(“rel”)-+$(a).attr(“rel”); }); $(“#建议”).html(组合的.slice(0,5).join(“”); 合并=[]; });

    }))

    首先将secong getJSON包装起来,如下所示。代码不错

    var combined = [];
    $(document).ready(function(){
    $("#search").keyup(function(){
        $("#suggest").html("");
        $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?",function(data){
            for(var key in data[1]){
                if(data[4]["google:suggesttype"][key]=="NAVIGATION"){
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'><a href='"+data[1][key]+"'>"+data[2][key]+"</a></li>");
                }else{
                    combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'>"+data[1][key]+"</li>");
                }
            }
    
                    $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?",function(data){
            for(var key in data.result){
                combined.push("<li rel='"+Math.round(data.result[key].score*5)+"'> Freebase: "+data.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(函数(){
    $(“#建议”).html(“”);
    $.getJSON(“http://suggestqueries.google.com/complete/search?q=“+$(“#搜索”).val()+”&client=chrome&callback=?”,函数(数据){
    用于(var键入数据[1]){
    如果(数据[4][“google:suggesttype”][键]=“导航”){
    组合.push(“
  • ”); }否则{ 组合。推送(“
  • ”+data[1][key]+“
  • ”; } } $.getJSON(“https://www.googleapis.com/freebase/v1/search?query=“+$(“#搜索”).val()+”&limit=3&encode=html&callback=?”,函数(数据){ for(数据中的var键。结果){ 组合式.push(“
  • Freebase:“+data.result[key].name+”
  • ”; } }); }); 组合排序(函数(a,b){ return+$(b).attr(“rel”)-+$(a).attr(“rel”); }); $(“#建议”).html(组合的.slice(0,5).join(“”); 合并=[]; });

    }))

    实际上,它确实返回值,但是这里有一个时间问题。在请求实际完成之前,您用结果填充列表。请尝试以下方法:


    另外,由于您要将第二个请求的结果附加到数组中,它们将永远不会显示,因为您的
    .slice(0,5)
    ,所以我删除了它。

    实际上,它确实返回值,但这里有一个计时问题。在请求实际完成之前,您用结果填充列表。请尝试以下方法:


    另外,由于您正在将第二个请求的结果附加到数组中,因此由于
    .slice(0,5)
    ,它们将永远不会显示,因此我删除了它。

    我注意到了代码的两个方面

    排序和追加应该在ajax函数的回调中调用,这可以通过生成另一个处理排序和显示的函数来实现。然后在成功回调中调用此函数

    第二,freemarker结果会显示出来,但是它们总是被发送到列表的底部。如果您查看200个结果,它们位于底部

    var combined = [];
    $(document).ready(function(){
        $("#search").keyup(function(){
            $("#suggest").html("");
            $.getJSON("http://suggestqueries.google.com/complete/search?q="+$("#search").val()+"&client=chrome&callback=?",function(data){
                for(var key in data[1]){
                    if(data[4]["google:suggesttype"][key]=="NAVIGATION"){
                        combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'><a href='"+data[1][key]+"'>"+data[2][key]+"</a></li>");
                    }else{
                        combined.push("<li rel='"+data[4]["google:suggestrelevance"][key]+"'>"+data[1][key]+"</li>");
                    }
                }
                sortAndDisplay(combined);
            });
            $.getJSON("https://www.googleapis.com/freebase/v1/search?query="+$("#search").val()+"&limit=3&encode=html&callback=?",function(data){
                for(var key in data.result){
                    combined.push("<li rel='"+Math.round(data.result[key].score*5)+"'> Freebase: "+data.result[key].name+"</li>");
                }
                sortAndDisplay(combined);
            });
        });
    });
    
    function sortAndDisplay(combined){
            combined.sort(function(a,b){
                 return +$(b).attr("rel") - +$(a).attr("rel");
            });
            $("#suggest").html(combined.slice(0, 200).join(""));
            combined = [];
    }
    
    var组合=[];
    $(文档).ready(函数(){
    $(“#搜索”).keyup(函数(){
    $(“#建议”).html(“”);
    $.getJSON(“http://suggestqueries.google.com/complete/search?q=“+$(“#搜索”).val()+”&client=chrome&callback=?”,函数(数据){
    用于(var键入数据[1]){
    如果(数据[4][“google:suggesttype”][键]=“导航”){
    组合.push(“
  • ”); }否则{ 组合。推送(“
  • ”+data[1][key]+“
  • ”; } } 排序显示(组合); }); $.getJSON(“https://www.googleapis.com/freebase/v1/search?query=“+$(“#搜索”).val()+”&limit=3