繁体   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