2013-07-26 38 views
1

我在這裏要求jQuery ajax()中有任何設置參數,它讓我們在數據成功傳輸到php文件時調用函數,而不是在數據傳輸時和PHP文件解析代碼並返回一個值。當數據傳輸到php文件時調用一個函數

我有下面的代碼:

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

但我想它想:

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

回答

0

你可能會使用這樣的

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

欲瞭解更多詳情,請查看以下鏈接

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

1

您只能從服務器端得到1個答案,成功與否有什麼問題?如果過程時間太長,那麼你可以儘快得到它的開始,並顯示「處理您的請求」,作爲結果,從腳本返回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 

你的腳本將繼續運行,而你從收到成功應答它。

+0

是的,我知道總會有一個返回值。你猜猜我想要什麼是對的,但問題在於我對PHP並不擅長。我甚至不知道你的代碼是幹什麼的。我只知道在PHP中使用變量和控制結構。它可以簡單一點嗎? – Heart

+0

這只是需要返回到接收數據的ajax。只要把它加到你的php腳本。 –

+0

那麼當腳本服務器端運行完畢後,有什麼辦法可以獲得知情的客戶端? –

相關問題