1

For some reason my function is returning undefined while seemingly working in itself.

function getDomains() {
    $.ajax({
        url:    '/rewrites/cgi-bin/ajax.pl?action=listdomains',
        dataType:'json',
        async:  false,
        success: function( data ) {
            if (data.error) {
                alert(data.error);
            }
            else {
                alert(data.domains);
                return(data.domains);
            }
       }
   });
}

alert(getDomains());

My first alert shows a populated list but the second is undefined. Does this make any sense?

t0mmyt
  • 493
  • 1
  • 7
  • 14
  • 7
    Even though it is synchronous, you still can't return from a callback. – Kevin B May 30 '13 at 14:31
  • 3
    Your `return` statement is for the `success` callback - you're not returning anything from `getDomains`. You could put `var domains;` at the top of your function. Then, in your callback, use `domains = data.domains;`, and then put `return domains;` at the end of your function – Ian May 30 '13 at 14:31
  • @Alnitak how do you spell it: SJAX? – A. Wolff May 30 '13 at 14:37
  • 1
    Read this for more information: http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call – nene May 30 '13 at 14:37
  • @nene great post, OP should read it! – TecHunter May 30 '13 at 14:40

2 Answers2

2

You're in a function for the success call. That's why you're not getting a result from your getDomains function. Assign it to a variable and return the variable after the ajax call.

function getDomains() {
    var results;
    $.ajax({
        url:    '/rewrites/cgi-bin/ajax.pl?action=listdomains',
        dataType:'json',
        async:  false,
        success: function( data ) {
            if (data.error) {
                alert(data.error);
            }
            else {
                alert(data.domains);
                results = data.domains;
            }
       }
   });
   return results;
}

alert(getDomains());
JustinMichaels
  • 1,092
  • 7
  • 12
  • 1
    Did you know you're alerting the results twice? – Blazemonger May 30 '13 at 14:33
  • 2
    I simply took his code and showed him how to assign the results and return the domains from his function. There was nothing in the question about removing multiple alerts. I left that there to affirm that the getDomains() function actually returned an array and wasn't undefined as he previously stated. – JustinMichaels May 30 '13 at 14:35
  • 1
    I coded the alerting twice so I could compare data at the different points in the function. – t0mmyt May 30 '13 at 14:38
1

Why don't you just do this, assuming you need your return for a function called whateverFunc():

function getDomains() {
    $.ajax({
        url:    '/rewrites/cgi-bin/ajax.pl?action=listdomains',
        dataType:'json',
        async:  false,
        success: function( data ) {
            if (data.error) {
                alert(data.error);
            }
            else {
                whateverFunc(data.domains);
            }
       }
   });
}


function whateverFunc(domains){
    alert(domains);
}

You can't return anything from success callback, it makes no sense. I would like also to complain about the async:false here. Why do you need it absolutely? You should let the call be async and manage the blocked state by yourself with some mutex or something around. In fact, you should trigger popup or whatever you need to do after you get the answer in the whateverFunc(). It's clean and you keep control on the blocking state.

TecHunter
  • 6,091
  • 2
  • 30
  • 47