0

I want the following function to return true or false based on my logic, but the function returning undefined.

function getUserData() {
    $.ajax({
    type: 'POST',
    contentType: 'application/json',
    url: 'myservice',
    dataType: "JSON",
    success: function (data) {
        if (data.d.UserID > 0) {
            return true;
        } else {
            return false
        }
        return false;
    },
    error: function (error) {
        return false;
    }
});};
if (getUserData()) {
//do something
} else {
//redirect
}

please do not consider it a duplicate, it is different, I dont want to return the callback functions I want to return true or false based on those callback functions.

Khalil
  • 119
  • 1
  • 14
  • Please put the action code, from where you are getting the data or at least show us what output/response you are getting from the server. – Abhishek Dhanraj Shahdeo Mar 15 '16 at 08:33
  • server is doing fine and returning my data, but I am unable to return these true or false to getUserData() – Khalil Mar 15 '16 at 08:40
  • Yesndreas you are right. but I want to decide my logic here in this function not want to repeat my logic everywhere – Khalil Mar 15 '16 at 08:45
  • It's asynchronous, so by definition, you *can't* get the result back until it's finished. That's what the *duplicate question* explains in depth: http://stackoverflow.com/a/14220323/2181514 – freedomn-m Mar 15 '16 at 08:57
  • It's not clear why you think you need to "repeat logic everywhere" - that's what the callback function is for - you pass the logic function into the ajax wrapper. It's explained in the duplicate at this point: *"callback will refer to the function we pass to foo when we call it and we simply pass it on to success"* – freedomn-m Mar 15 '16 at 08:59
  • I've to put my head down over this answer. I had not enough time to understand that so put my problem for direct solution – Khalil Mar 15 '16 at 09:43

2 Answers2

0

Try this:

function getUserData(callback) {
    $.ajax({
        type: 'POST',
        contentType: 'application/json',
        url: 'myservice',
        dataType: "JSON",
        success: function (data) {
            if (data.d.UserID > 0) {
                callback(true);
            } else {
                callback(false);
            }
            callback(false);
        },
        error: function (error) {
            callback(false);
        }
    });
};

getUserData(function(status) {
    if (status) {
    //do something
    } else {
    //redirect
    }
});
Deepak Biswal
  • 4,280
  • 2
  • 20
  • 37
0

Use the below code may solve your issue: create a function and it should returns the defferd object.

  function AJAX(type,url){

   this.type=type;      // EX: GET , POST
   this.url=url;

this.invoke = function(){
var deferred = $.Deferred();
$.ajax({
    type: this.type,
    url: this.url,      
    contentType: 'application/json',
    dataType:"JSON"
    success: function(response){
        deferred.resolve(response);
    },
    error: function(exception){
        console.log("Ajax call returned error.");
         deferred.reject(exception);
    }
});

return deferred;

};
}

now call this function with proper parameters as below

 var ajax=new AJAX("POST","Your action");
 ajax.invoke().then(function(data){
       if(data==true){
         // Your code for true
          }else{
         // your code for false
         }
  });

now you can modify the functions as per your needs..

Nalla Srinivas
  • 913
  • 1
  • 9
  • 17