Romi
Romi

Reputation: 4921

problem facing in javascript function returning value

I have two javascript functions. from validateSearchStirng() i am calling checkSolrServerAvailibility(), from which i am expecting to return flag=1 if ajax request is successful otherwise flag=0. but each time checkSolrServerAvailibility() returning flag=0. Why so and what i need to do for getting correct result

 function validateSearchStirng(sort,order,itemPerPage,showPage)
    {
         var serverflag;
serverflag=checkSolrServerAvailibility(sort,order,itemPerPage,showPage,query,solrURL);
         if(serverflag==0)
         {
        var msg= "<hr /><font size="+size+" ><b>Solr Server Not Runing </b></font><hr /> ";
        removeList();
        $("#result").html(msg); 
         }  
         if(serverflag==1)
         {
           getSolrResponse(sort,order,itemPerPage,showPage,query,solrURL);  
         }

    }


 function checkSolrServerAvailibility(sort,order,itemPerPage,showPage,query,solrURL)
    {
        var start=itemPerPage*(showPage-1);
        var end=itemPerPage;
        var flag=0
        $.ajax({
            url: solrURL+"/solr/db/select/?qt=dismax&wt=json&&start="+start+"&      rows="+end+"&q="+query+"&json.wrf=?",
            async:true,
            dataType: 'json',
            success: function(json){
            flag=1;
                return(flag);
            }
        })
       return(flag);

    }



} 

Upvotes: 1

Views: 133

Answers (3)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to redesign your solution to something like

function checkSolrServerAvailibility(sort,order,itemPerPage,showPage,query,solrURL, success, error){
     $.ajax({
            url: "",
            dataType: 'json',

            success: function(json){
                success.apply(this, arguments);
            },
            error:function(){
                error.apply(this, arguments);
            }
        })
}

function onAvailabilityCheck(json, textStatus, jqXHR){
    getSolrResponse(sort,order,itemPerPage,showPage,query,solrURL);  
}

function onAvailabilityCheckError(jqXHR, textStatus, errorThrown){
    var msg= "<hr /><font size="+size+" ><b>Solr Server Not Runing </b></font><hr /> ";
    removeList();
    $("#result").html(msg); 
}

function validateSearchStirng(sort,order,itemPerPage,showPage){
         checkSolrServerAvailibility(sort,order,itemPerPage,showPage,query,solrURL, onAvailabilityCheck, onAvailabilityCheckError);
}

Since you are working with ajax request which by definition asynchronous in nature, your code will not work because once the client sent the ajax request to server it will not wait for the server response to comeback. It will keep executing the next lines... that means your flag will always be set to false.

To solve this problem my suggestion will be is to use callback methods to handle both success and failure cases as shown.

In the case of a successful ajax request the onAvailabilityCheck method will be called with the given parameters and if the ajax request is a failure the onAvailabilityCheckError method will be called.

Upvotes: 0

Tadeck
Tadeck

Reputation: 137340

You should probably forget about using JS functions this way and do it using callbacks. So basically instead of doing something like that:

serverflag=checkSolrServerAvailibility(...);

do something like that:

var processServerFlagCallback = function(flag){
    // do something with your flag
}

checkSolrServerAvailibility(..., processServerFlagCallback);

and inside checkSolrServerAvailability, when you have information about success, invoke given callback with 1 as parameter, and in case of error - with 0 as parameter.

To see how to determine that an error occured during request, please see jQuery's .ajax() documentation on error parameter.

Upvotes: 0

Jatin Dhoot
Jatin Dhoot

Reputation: 4364

async : false

You should turn this flag to false.

Then you will get the desired results

Upvotes: 1

Related Questions