在ajax调用中访问函数外部的变量的问题

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

我之前发布了与此问题相关的问题: 访问javascript代码中if块之外的变量更改值的问题 ,我得到了我必须使ajax调用异步。 所以我确实喜欢上面的代码,但我仍然没有在if块之外获得更新的newquery。 它仍然显示了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){ 

现在因为我想在$ getjosn()中使用这个更新的newquery,如果result.response.numFound == 0,否则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 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){ } 

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

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

您正在重新定义ajax()成功函数中的’result’。 改变这一点,然后努力解决你的问题:)

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

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

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

根据迈克尔·赖特的回答得到的新答案:

 $(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 }