0

I need to return true or false to my alert() from the results of an ajax success function. However, I'm having problems since the alert() result is always 'undefined'...any ideas on how to fix this?

I have DoAjax as it's own function because I call it multiple times within the .js file.

var val = '12345';  
alert( DoSerialVerification( val ) ); //this returns 'undefined' regardless of cType.length

function DoSerialVerification( piVal ){
    var fSuccess = function( oData ){
        var cType = oData.type
        if( cType.length > 0 ){
           alert( cType );                  //with piVal = 12345 cType will return 'MODULE'
           $('#dialog-1').attr('type', cType);
           return true;
        } else {
           return false;
        }
    };
    DoAjax({ Action: "DoSerialVerification", Value: piVal }, fSuccess );
}

function DoAjax( pAjaxParams, pSuccess ){
    $.ajax({url        : 'procedures?',
            data       : pAjaxParams,
            async      : false,
            type       : "POST",
            dataType   : "json",
            error      : function(oD,oT,oE){ alert( oD+'\n'+oT+'\n'+oE ) },
            success    : pSuccess
    });
}
2
  • Did you verify procedures? is returning data? Commented Sep 15, 2011 at 21:25
  • @IAbstractDownVoteFactory - yes procedures is returning 'MODULE' Commented Sep 16, 2011 at 12:14

2 Answers 2

1

you need to return something from the DoSerialVerification function... modified DoSerialVerification function:

function DoSerialVerification( piVal ){
    // here
    var result = false;

    var fSuccess = function( oData ){
        var cType = oData.type
        if( cType.length > 0 ){
           alert( cType );                  //with piVal = 12345 cType will return 'MODULE'
           $('#dialog-1').attr('type', cType);

           //here
           result = true;
        }
    };
    DoAjax({ Action: "DoSerialVerification", Value: piVal }, fSuccess );

    //here
    return result;
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Elliot Nelson: jQuery supports synchronous ajax, "async: false" option is used for that.
'cause DoSerialVerification returned nothing but it had to. i guess you expected nested fSuccess function to propagate its result to DoSerialVerification... but no, result of fSuccess is not propagated.
0

What you're trying to do isn't possible: calling AJAX is an asynchronous process, your function DoAjax will return before the response is ever received from the server. That is why you are providing callbacks: once the server returns a response, it'll enter either your error callback or your success callback, depending on the result.

Change this:

alert( DoSerialVerification( val ) );

To this:

DoSerialVerification(val);

And then move your alert call into your "fSuccess" function.

Comments

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.