Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在父函数中的ajax post后返回true或false?_Javascript_Jquery_Ajax_Asynchronous_Return - Fatal编程技术网

Javascript 在父函数中的ajax post后返回true或false?

Javascript 在父函数中的ajax post后返回true或false?,javascript,jquery,ajax,asynchronous,return,Javascript,Jquery,Ajax,Asynchronous,Return,在进行此ajax调用后,我想返回true或false: function write_csv(data, path, file) { $.ajax({ url: 'functions.php', type: 'POST', data: { operation: 'SAVE_CSV', save_path: path, save_file: file,

在进行此ajax调用后,我想返回true或false:

function write_csv(data, path, file) {

    $.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');

            return true; /* <-- */

        }
    });
}
我知道它是异步的,但也许有人有另一个想法。
如果之类的东西……

你可以通过承诺或回访来实现你想要的,我不会用
来做

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); /* <-- */

        }
    });
}

我将脱离jQuery惯例,给您一个“jQuery”答案,因为这就是您正在使用的

在jQuery中,您可以在大多数jQuery方法中传入回调(一个在实际使用的函数完成后“调用”的函数)。jQuery的约定是将回调作为传入函数的最后一个参数。在您的示例中,
write_csv()
函数的最后一个参数是一个额外的回调,如下所示:

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);
        }
        error: function(result){
            console.log('async failed');
            callback(false);
        } 
    });
}
请注意传入的
错误
键以及对
$.ajax()
函数中的
成功
键所做的更改

现在,当您想在if条件语句中使用异步函数时,可以使用

write_csv(my_data, my_path, 'export.csv', function(response){
    if(response === true){
        go_on()
    }
    else if(response === false){
        // do something else
    }
});
可能重复的
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);
        }
        error: function(result){
            console.log('async failed');
            callback(false);
        } 
    });
}
write_csv(my_data, my_path, 'export.csv', function(response){
    if(response === true){
        go_on()
    }
    else if(response === false){
        // do something else
    }
});