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

发布于 2024-11-16 08:54:49 字数 2488 浏览 0 评论 0原文

$.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

月下伊人醉 2024-11-23 08:54:49

试试这个:

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

            }

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

            }
遗心遗梦遗幸福 2024-11-23 08:54:49

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

清引 2024-11-23 08:54:49

您正在 ajax() success 函数中重新定义“结果”。更改此设置,然后努力解决您的问题:)

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

情仇皆在手 2024-11-23 08:54:49

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

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

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

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.

赤濁 2024-11-23 08:54:49

基于迈克尔赖特的答案的新答案:

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

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文