开发者

problem in accesing a variable outside of a function in ajax call

$.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;

                    });
                    }
                });
}

I posted question related to this problem previously: Problem in accessing a variable's changed value outside of if block in javascript code and i got that i have to make ajax call async. So i did like the above code, but still i am not getting updated newquery outside of if block. still it is showing the old value of newquery. please suggest where i ma doing wrong

edit

$(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({
            开发者_Python百科        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){

Now as i want to use this updated newquery in $getjosn() if result.response.numFound==0,otherwise newquery will hold the old value


Try this:

$(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){

            }


The $.ajax(...) call returns immediatly. The success function is a callback function which means that this function is called when the ajaxrequest completes. If you want to change something with the new values recieved you have to do that in the success function.

A second point is, you overwrite your value for newquery with each loop, so newquery will only hold the last element of your result.speelcheck.suggestions list. Not sure if that is what you want.


You are redefining 'result' in the ajax() success function. Change this, and then work on fixing your problem :)


You want to call the getJSON() function within the success function of the $.ajax() request. The success() event isn't called until the data has been returned, this won't happen straight away, and so the final getJSON() event will fire before this.

Moving the getJSON() function to the end of the $.ajax() success function will resolve your problem.

Ensure it's outside the $.each() statement.


new answer based on answer from 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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜