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

2023-12-05

$.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 的旧值。 请建议我哪里做错了

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

现在我想在 $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){

            }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐