Promote callback onSuccess return value to the Caller Function return value

前端 未结 3 743
星月不相逢
星月不相逢 2020-12-17 02:19

I have a javascript function that calls a generic function to make an ajax call to the server. I need to retrieve a result (true/false) from the callback function of the aja

3条回答
  •  天涯浪人
    2020-12-17 02:54

    Just found how to do it :) Declaring a variable and updating it accordingly from the callback function. Afterwards I can return that variable. I place the code for future readers:

    function CallServer(urlController) {
        var returnValue = false;
        $.ajax({
            type: "POST",
            url: urlController,
            async: false,
            data: $("form").serialize(),
            success:
                function(result) {
                    if (someLogic){
                        returnValue = true;
                        return;
                    }
                },
            error:
                function(errorThrown) {
                    alert("Error occured: " + errorThrown);
                }
            });
    
            return returnValue;
    }
    

提交回复
热议问题