在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 将保留旧值
$.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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
Try this:
$.ajax(...)
调用立即返回。 success函数是一个回调函数,这意味着当ajax请求完成时调用该函数。如果您想使用收到的新值更改某些内容,则必须在 success 函数中执行此操作。第二点是,您会在每个循环中覆盖 newquery 的值,因此 newquery 将仅保存
result.speelcheck.suggestions
列表的最后一个元素。不确定这是否是您想要的。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.您正在
ajax()
success 函数中重新定义“结果”。更改此设置,然后努力解决您的问题:)You are redefining 'result' in the
ajax()
success function. Change this, and then work on fixing your problem :)您想要在getJSON() 事件将在此之前触发。
$.ajax()
请求的 success 函数中调用getJSON()
函数。在返回数据之前,不会调用success()
事件,这不会立即发生,因此最终的将
getJSON()
函数移动到$.ajax()
success 函数的末尾将解决您的问题。确保它位于
$.each()
语句之外。You want to call the
getJSON()
function within the success function of the$.ajax()
request. Thesuccess()
event isn't called until the data has been returned, this won't happen straight away, and so the finalgetJSON()
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: