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?

5
  • 7
    Even though it is synchronous, you still can't return from a callback. Commented May 30, 2013 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 Commented May 30, 2013 at 14:31
  • @Alnitak how do you spell it: SJAX? Commented May 30, 2013 at 14:37
  • 1
    Read this for more information: stackoverflow.com/questions/14220321/… Commented May 30, 2013 at 14:37
  • @nene great post, OP should read it! Commented May 30, 2013 at 14:40

2 Answers 2

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());
Sign up to request clarification or add additional context in comments.

3 Comments

Did you know you're alerting the results twice?
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.
I coded the alerting twice so I could compare data at the different points in the function.
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.

4 Comments

This way, no need to set async false
This is a function that happens early on and is a blocker for any other functionality. It is the only non async piece of nastiness.
@user2245703 Ha, ok, blocking UI is expected behaviour, makes more sense
yeah I really hate sync $.ajax calls but at least it's cleaner this way :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.