且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在父函数中的ajax发布后返回true或false?

更新时间:2022-06-13 02:33:12

您可以使用Promises或回调来完成所需的操作.

You can use Promises or callbacks to accomplish what you want.

function write_csv(data, path, file, callback) {

    $.ajax({
        url: 'functions.php',
        type: 'POST',
        data: {
            operation: 'SAVE_CSV',
            save_path: path,
            save_file: file,
            save_string: data
        },
        success: function(result) {
            console.log('write_file(' + path + file + '); -done');

            callback(true); /* <-- */

        }
    });
}

并且:

function make_csv () {

    /* 
    |
    V
    */

    function go_on() {
        alert('YEAH!');
    }

    write_csv(my_data, my_path, 'export.csv', function(result) {
        if (result == true) {
            go_on();
        }
    });
}