【问题标题】:problem in accesing a variable outside of a function in ajax call在ajax调用中访问函数外部变量的问题
【发布时间】:2011-06-22 07:02:34
【问题描述】:
$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?", function(result){
            //$.each(result.response.docs, function(result){




                if(result.response.numFound==0)
                {


                $.ajax({
                    url: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                    async:false,
                    success: function(result){
                    $.each(result.spellcheck.suggestions, function(i,item){
                        newquery=item.suggestion;

                    });
                    }
                });
}

我之前发布了与此问题相关的问题:Problem in accessing a variable's changed value outside of if block in javascript code 我知道我必须使 ajax 调用异步。所以我确实喜欢上面的代码,但我仍然没有在 if 块之外获得更新的 newquery。它仍然显示新查询的旧值。 请提出我哪里做错了

编辑

$(document).ready(function(){
// This function get the search results from Solr server 
    $("#submit").click(function(){
        var query=getquerystring() ; //get the query string entered by user
        // get the JSON response from solr server 
        var newquery=query;

$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?", function(result){
            //$.each(result.response.docs, function(result){

            if(result.response.numFound==0)
                    {


                $.ajax({
                    url: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                    async:false,
                    dataType: 'json',

                    success: function(json){
                    $.each(json.spellcheck.suggestions, function(i,item){
                        newquery=item.suggestion;

                    });
                    }

                });

                }


    $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(result){

现在,如果 result.response.numFound==0,我想在 $getjosn() 中使用这个更新后的 newquery,否则 newquery 将保留旧值

【问题讨论】:

    标签: javascript jquery ajax json


    【解决方案1】:

    试试这个:

    $(document).ready(function(){
        // This function get the search results from Solr server 
        $("#submit").click(function(){
            var query=getquerystring() ; //get the query string entered by user
            var newquery=query;
            $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?", function(result){
                if(result.response.numFound==0)
                {
                    $.ajax({
                        url: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                        async:false,
                        dataType: 'json',
                        success: function(json){
                            $.each(json.spellcheck.suggestions, function(i,item){
                                newquery=item.suggestion;
                            }); 
                            $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(result){
                        }
    
                        });
                    }
                }else{
    
                    $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", function(result){
    
                }
    

    【讨论】:

    • 您可以将 success() 函数作为两个 getJSON 调用的通用函数。
    • 哦,更改$.getJSON() 函数中的最终result 变量。
    • 迈克尔·赖特:非常感谢。你能告诉我如何制作一个通用的回调函数
    • 更改:函数(结果){ 为:commonCallback();然后创建函数 commonCallback(feedback){ } 等。
    • Michael Wright:是的,我得到它是为了实现一个通用功能。但是我应该把这个通用函数放在哪里
    【解决方案2】:

    $.ajax(...) 调用立即返回。 success 函数是一个回调函数,这意味着该函数在 ajaxrequest 完成时被调用。如果您想使用收到的新值更改某些内容,则必须在成功函数中执行此操作。

    第二点是,您会在每个循环中覆盖 newquery 的值,因此 newquery 将只保存 result.speelcheck.suggestions 列表的最后一个元素。不确定这是否是您想要的。

    【讨论】:

    • Fender:我想在 if 块之外使用 newquery,这就是我使用 acync:false 的原因。而且我只想获取循环接收到的最后一个值,但在 if 块之外
    • 当我在 $.ajax({ 之后放置一个 alert() 时,我可以在 if 块之外获取这个 newquery 更新的值,但不能没有 alert() :(
    • 你在哪里声明了变量newquery
    • @Romi:你只做了一个同步调用,你还有一个异步的$.getJSON。但是,您可以只取代码的最后一行(新的 $.getJSON 并将其放在 $.each 内的 $.ajax 内),而不是使它们异步。
    • 不要把它放在each 中,而是把它放在它后面,因为你只想使用最后收到的值。那么也没有必要再使这个呼叫同步了。
    【解决方案3】:

    您正在ajax() 成功函数中重新定义“结果”。改变这个,然后努力解决你的问题:)

    【讨论】:

    • Michael Wright:正如您在我的编辑中看到的,我将结果更改为 json。但问题仍然是一样的:(
    • 他表示成功函数中的变量名
    • Fender:对不起,我没能得到。有 i 和 item 但他们只是在 ajax 成功
    • 我们的意思是最后一个 $.getJSON(..) 有一个名为 result 的变量,它可能与您的第一个 getJSON 中的变量 result 冲突
    【解决方案4】:

    您想在$.ajax() 请求的成功函数中调用getJSON() 函数。在数据返回之前不会调用success() 事件,这不会立即发生,因此最终的getJSON() 事件将在此之前触发。

    getJSON() 函数移动到$.ajax() 成功函数的末尾将解决您的问题。

    确保它在$.each() 语句之外。

    【讨论】:

    • Michael Wright:再看代码getJson函数是不是ajax成功了。即使它在 if 块之外。
    • 否... 将最后的 getJSON() 移到 $.ajax() 成功函数内,就在最后。
    • Michael Wright:那么最终getJson是否会在response.numFound=0时才通过第一个getJson获取更新后的newquery的值??
    【解决方案5】:

    基于 michael wright 的答案的新答案:

    $(document).ready(function(){
        // This function get the search results from Solr server 
        $("#submit").click(function(){
            var query=getquerystring() ; //get the query string entered by user
            var newquery=query;
            $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?", function(result){
                if(result.response.numFound==0)
                {
                    $.ajax({
                        url: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                        async:false,
                        dataType: 'json',
                        success: commonSuccess});
                }else{
    
                    $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price asc&hl=true&hl.fl=description&hl.usePhraseHighlighter=true&json.wrf=?", commonSuccess);
    
                }
    //...
    }); //End of $(document).ready(...)
    
    function commonSuccess(json){
        //do onSuccess for all queries
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-05-25
      • 2014-01-29
      • 1970-01-01
      • 2011-04-15
      • 2020-04-26
      • 2020-10-30
      相关资源
      最近更新 更多