当数据传输到php文件时,调用一个函数


Call a function when data is transfered to php file

我在这里想问的是,jQuery ajax()中是否有任何设置参数,允许我们在数据成功传输到php文件时调用函数,而不是在数据传输和php文件解析代码并返回值时调用函数。

我有以下代码:

$.ajax({
    ..
    ..
    ..
    success: function(data) {
        // do something
    }
});

但我想要的是:

$.ajax({
    ..
    ..
    ..
    onTransfer: function() {
        // do something like show a div saying "Done".
    },
    success: function(data) {
        // do something
    }
});

服务器端只得到一个答案,成功有什么错?如果您的过程耗时太长,那么您可以在脚本启动后立即返回,并显示"正在处理您的请求"作为success:的结果

<?php
    ob_end_clean();                    // discard everything
    header("Connection: close");       // send Connection close
    ignore_user_abort(true);           // ignore user abort
    set_time_limit(0);                 // no time limit
    ini_set("memory_limit","200M");    // setup a memory usage needed
    ob_start();                        // start output buffer
    header("Content-Length: 0");       // send lenght 0
    ob_end_flush();                    // end and flush
    flush();
    // continue your script

您的脚本将继续运行,同时收到它的成功答复。

您可以像这样使用

$.ajax({
   url: "test.html",
   context: document.body
   }).done(function() {
   $(this).addClass("done");
   });

有关更多详细信息,请查看以下链接

http://api.jquery.com/jQuery.ajax/