本文介绍了在Ajax调用accesing一个变量外的函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<$p$p><$c$c>$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?",功能(结果){            //$.each(result.response.docs,功能(结果){                如果(result.response.numFound == 0)                {                $阿贾克斯({                    网址: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",                    异步:假的,                    成功:函数(结果){                    $每个(result.spellcheck.suggestions,功能(I,项目){                        newquery = item.suggestion;                    });                    }                });}

我贴的问题与此相关的问题previously:Problem在访问if块在javascript code 以外的变量的当前值,并我得到了,我不得不作出AJAX调用异步。所以,我没有像上面code,但我没有得到更新newquery如果块外。还是它显示newquery的旧值。请建议在那里我是个做错了

修改

  $(文件)。就绪(函数(){
//这个函数得到Solr的服务器搜索结果
    $(#提交)。点击(函数(){
        变种查询= getquerystring(); //获取查询字符串由用户输入
        //从Solr的服务器的JSON响应
        VAR newquery =查询;

$.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?",功能(结果){
            //$.each(result.response.docs,功能(结果){

            如果(result.response.numFound == 0)
                    {


                $阿贾克斯({
                    网址: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                    异步:假的,
                    数据类型:JSON,

                    成功:函数(JSON){
                    $每个(json.spellcheck.suggestions,功能(I,项目){
                        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 =真放; hl.fl =描述&放大器; hl.usePhraseHighlighter =真放; json.wrf =,函数(结果){
 

现在,我想用在$ getjosn()这个更新newquery如果result.response.numFound == 0,否则newquery将持有旧值

解决方案

试试这个:

  $(文件)。就绪(函数(){
    //这个函数得到Solr的服务器搜索结果
    $(#提交)。点击(函数(){
        变种查询= getquerystring(); //获取查询字符串由用户输入
        VAR newquery =查询;
        $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&json.wrf=?",功能(结果){
            如果(result.response.numFound == 0)
            {
                $阿贾克斯({
                    网址: "http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=100&q="+query+"&spellcheck=true&json.wrf=?",
                    异步:假的,
                    数据类型:JSON,
                    成功:函数(JSON){
                        $每个(json.spellcheck.suggestions,功能(I,项目){
                            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 =真放; hl.fl =描述&放大器; hl.usePhraseHighlighter =真放; json.wrf =,函数(结果){
                    }

                    });
                }
            }其他{

                $.getJSON("http://192.168.1.9:8983/solr/db/select/?wt=json&&start=0&rows=20&q="+newquery+"&sort=price ASC&安培; HL =真放; hl.fl =描述&放大器; hl.usePhraseHighlighter =真放; json.wrf =,函数(结果){

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

解决方案

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

            }

这篇关于在Ajax调用accesing一个变量外的函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 09:38