0

I have a java script function to get some data from database.

function getSummary() { 
 var entries;
 var totalRowCount;
 $.ajax({
     type: "GET",
     url: "myurl",
     contentType: "application/json; charset=utf-8",
     dataType: "json",
     success: function (data, status, jqXHR) {
           entries= data;           

           // some logic here

            for(var key in data) {
                if(data.hasOwnProperty(key)) {
                    totalRowCount = data[key];
                    break;
                }
            }                  
     },

     error: function (jqXHR, status) {           
         alert('');          
     }        
 });
return(totalRowCount);
};

When i access the return value from above function in jsp page(var x=getSummary();) ,it says variable undefined.Can some one help me to fix the code?

Sona Shetty
  • 997
  • 3
  • 18
  • 41
  • Because it's a ajax doesn't work synchronously, when you try to access `x` request may have not yet completed. – Pradeep Simha Feb 12 '17 at 07:47
  • You need to restructure the call to `getSummary()` so the action taken on the return value is done within the `success` callback. – shmosel Feb 12 '17 at 07:58

0 Answers0