0
$.namespace('MyClass');
MyClass.setDataResponse: function(URL, dataObj, successcallback, failurecallback){
        $.ajax({
            url: URL,
            async: false,
            type: 'POST',
            data: dataObj,
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            success : successcallback,
            error : failurecallback
        });
    }

var dataObj = '{"id": "5"}';
function SuccessCall(res){
  return res;
}
var getdata = MyClass.setDataResponse('requesturl', dataObj, SuccessCall);

console.log(getdata);

Why getdata is undefined... while in success it is return obj.

how can achieve getdata as return object..

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Tameshwar
  • 789
  • 1
  • 10
  • 17
  • You can't `return` from an AJAX call - you use a callback. – tymeJV Dec 10 '14 at 15:35
  • 3
    FYI, a function does not have a `return` statement, it will always return `undefined`. `MyClass.setDataResponse` does not have a `return` statement hence it will return `undefined`. Even without the async function call that would be the case and should not be a surprise for you. – Felix Kling Dec 10 '14 at 16:24
  • 1
    Try not to use `async:false` – Bergi Dec 10 '14 at 16:26

1 Answers1

0

The functions that you execute after of obtain the response, cant return values.

$.ajax({
    'url' : 'someurl',
    'success' : function(response) {
      // cant return values;
      // it can work only inside of here
    }
});

Too

function dowork(response) {
      // cant return values;
      // it can work only inside of here    
}

$.ajax({
    'url' : 'someurl',
    'success' : dowork
});
ray
  • 171
  • 1
  • 11