Problem in accessing a variable's changed value outside of if block in javascript code
$("#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)
{
开发者_Python百科
$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?", function(result){
$.each(result.spellcheck.suggestions, function(i,item){
newquery=item.suggestion;
});
});
}
In the above javascript code a variable newquery initialy having value of query. but when the if condition is true its value have changed. but my problem is i am not getting its changed value outside of if block while i want this changed value. how can i do this.
Please reply.
EDIT
Can i make it as:
$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?"
{
async:false
},
function(result) {
The problem is that $.getJson is asynchron.
See this: How to download a text file and store as a string in jQuery
精彩评论