簡體   English   中英

瀏覽器收到200ok時中止或標記ajax請求

[英]abort or mark an ajax request once the browser receive 200ok

我有以下要求

// =============================================== start()
//
// clicked_button : the user click the start button.
// app_path       : the app path
//
// return none.
//
var start = function(clicked_button, app_path) {

    $.ajax({
        type: 'POST',
        data: { 'app_path': app_path },
        url: '/app/start',
        context: clicked_button,
    })

    .done(function(data) {
        console.log(data);
        $(this).removeClass('btn-success');
        $(this).addClass('btn-danger');
        $(this).text('stop');
    })

    .fail(function(data) {
        console.log('failed to start the app: ' + data);
    })
};

, the browser receive 200 OK, , but somehow the code in .done function is not executed, it's like the request is not completed yet. 該請求已發送到服務器,並且操作已完全完成, ,瀏覽器收到200 OK, ,但是以某種方式未執行.done函數中的代碼,就像該請求是還沒完成。

我如何檢查瀏覽器是否收到200 OK,然后中止ajax調用,然后繼續執行jquery代碼。

您不需要這樣做。

您可以只在ajax中使用成功功能。

  $.ajax({
        type: 'POST',
        data: { 'app_path': app_path },
        url: '/app/start',
        context: clicked_button,
        success: function(data){
           console.log(data);
           $(this).removeClass('btn-success');
           $(this).addClass('btn-danger');
           $(this).text('stop');
        },
        error: function(data){
           console.log('failed to start the app: ' + data);
        }
    })

試試這個代碼,看看螢火蟲有什么問題!

使用dataType:'json'以獲得服務器響應

我通過將這些行添加到我的php代碼中解決了這個問題:

sleep(1);
return Redirect::home();

為什么呢?

調試代碼后,看起來我請求的所有操作都已完成,所以唯一的問題是請求未決,或者以某種方式迷失了方向。

我最好的建議是因為我在代碼上使用了proc_open ..

仍然不是最好的解決方案,但是它正在工作並且很高興。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM