Promote callback onSuccess return value to the Caller Function return value

前端 未结 3 759
星月不相逢
星月不相逢 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:53

    Just in case you want to go the asynchronous way (which is a better solution because it will not freeze your browser while doing the request), here is the code:

    function CallServer(urlController, callback) {
        $.ajax({
            type: "POST",
            url: urlController,
            async: true,
            data: $("form").serialize(),
            success:
                function(result) {
                    var ret = ( someLogic );
                    callback(ret);
                },
            error:
                function(errorThrown) {
                    return false;
                }
        });
    }
    
    function Next() {
        CallServer("/Signum/TrySave", function(result) {
            if (result == true) {
                document.forms[0].submit();
            }
        });
    }
    

提交回复
热议问题