0

I have a function like shown below and it I am not able to return data value from the place I am calling the function.

  function checkifRS(){
        $.ajax({
                url: 'someURL.php',
                success: function(data) {
                                return data;
                            }
                });
    }

Any suggestions would be helpful...

Digant
  • 398
  • 3
  • 10
  • you meant to say you are not getting the data from `someURL.php` OR you want to display the data in any `
    `
    – Moeed Farooqui Mar 10 '14 at 07:55
  • No you can't do this.if you want to display the data then you have use the forloop for printing the data or dynamically add the tag that display yours data. – user3217843 Mar 10 '14 at 07:56
  • The callback function should be a function that actually does something with the data (e.g modifying the DOM and displaying it) instead of just returning it. – Kemal Fadillah Mar 10 '14 at 07:58
  • 1
    possible duplicate of [How to return the response from an AJAX call?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call) – Satpal Mar 10 '14 at 08:03
  • @MoeedFarooqui: I want to call this function from a third place and use the data value. – Digant Mar 11 '14 at 09:13

3 Answers3

0

well , one way is to use a callback function inside ajax success

function checkifRS(callback) {
    $.ajax({
        url: 'someURL.php',
        success: function(data) {
            callback(data); 
        }
    });

}

and call it this way,

 checkifRS(function(data){

     //do something with data
  });
bipen
  • 36,319
  • 9
  • 49
  • 62
0

Handle data like this

function checkifRS(handleData) {
      $.ajax({
                    url: 'someURL.php',
                    success: function(data) {
                                    handleData(data); 
                                }
                    });
    }

Get data in output "Call like this"

checkifRS(function(output){
  // here you use the output
});
jsduniya
  • 2,464
  • 7
  • 30
  • 45
0

use async: false

JavaScript runs in the UI thread of the browser and any long running process will lock the UI, making it unresponsive. Additionally, there is an upper limit on the execution time for JavaScript and the browser will ask the user whether to continue the execution or not. All of this is really bad user experience. The user won't be able to tell whether everything is working fine or not. Furthermore the effect will be worse for users with a slow connection.

 function checkifRS(){
        $.ajax({
                url: 'someURL.php',
                async: false,
                success: function(data) {
                                return data;
                                }
                });
    }
Anant Dabhi
  • 10,864
  • 3
  • 31
  • 49