1
$.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({
                    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

5 Answers 5

1

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

            }
Sign up to request clarification or add additional context in comments.

9 Comments

You can then chance success() function to be a common function for the two getJSON calls.
Oh and changing the final result variable in the $.getJSON() functions.
Michael Wright: Thanks a lot. would you please tell how can i make a common callback function
change: function(result){ to: commonCallback(); then create the function commonCallback(feedback){ } etc.
Michael Wright: Ya i got it to make a common function. but where should i put this common fuction
|
1

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.

9 Comments

Fender: I want to use newquery outside of if block , thats why i used acync:false. and ya i just want to get only last value received by the loop but outsidee of if block
When i put an alert() after $.ajax({, then i am able to get this newquery updated value outside of if block but not without alert() :(
where did you declare the variable newquery?
@Romi: You have only made one of the calls sync, you still have a $.getJSON that is async. However, instead of making them async, you can just take the last line of your code (the new $.getJSON and put it inside the $.each inside the $.ajax.
dont put it in the each but put it behind it as you just want to use the last recieved value. then there is also no need for making this call sync anymore.
|
0

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

4 Comments

Michael Wright: As you can see in my edit i changed result to json. but still the problem is same :(
he means the variable name in the success function
Fender: Sorry i could not get. there is i and item but they are only in ajax success
we mean the last $.getJSON(..) there is a variable named result which could conflict with the variable result from your first getJSON
0

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.

3 Comments

Michael Wright: re look at the code getJson function is outside of ajax success. even it is outside of if block.
No... Move the final getJSON() to within the $.ajax() success function, just at the end.
Michael Wright:Then will final getJson take the value of updated newquery only when result.response.numFound=0 by first getJson??
0

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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.