简体   繁体   English

数据传输到php文件时调用函数

[英]Call a function when data is transfered to php file

I am here to ask that is there any settings argument in jQuery ajax() which let us to call a function when data has transferred to php file successfully not when the data has transferred and php file parsed the code and returned a value. 我在这里要问的是,jQuery ajax()中是否有任何设置参数,可让我们在数据成功传输到php文件时调用一个函数,而不是在数据传输且php文件解析代码并返回值时调用该函数。

I have following code: 我有以下代码:

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

But I want it like: 但我想要这样:

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

You only get 1 answer from server side, what's wrong with success? 您仅从服务器端得到1个答案,成功有什么问题? If your process takes too long then you could return from script as soon as it get's started and show 'Processing your request' as result for success : 如果您的过程花费的时间太长,那么您可以在脚本启动后立即从脚本中返回,并显示“正在处理您的请求”作为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

Your script will continue to run while you receive a success answer from it. 当您收到脚本的成功回答时,脚本将继续运行。

You might use it like this 您可能会这样使用

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

For more details please check following link 有关更多详细信息,请检查以下链接

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM