简体   繁体   English

浏览器收到200ok时中止或标记ajax请求

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

i have the following request 我有以下要求

// =============================================== 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 request is sent to the server, and the operation is done completely, i've tested it , the browser receive 200 OK, i've check it , but somehow the code in .done function is not executed, it's like the request is not completed yet. 该请求已发送到服务器,并且操作已完全完成, 我已经对其进行了测试 ,浏览器收到200 OK, 我已经对其进行了检查 ,但是以某种方式未执行.done函数中的代码,就像该请求是还没完成。

how can i check if the browser receive a 200 ok, then just abort the ajax call, and continue the jquery code. 我如何检查浏览器是否收到200 OK,然后中止ajax调用,然后继续执行jquery代码。

You dont need to do that. 您不需要这样做。

You can just use the success function in ajax. 您可以只在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);
        }
    })

Try this code and check out you firebug for any problem! 试试这个代码,看看萤火虫有什么问题!

使用dataType:'json'以获得服务器响应

i solved this problem by adding these lines to my php code: 我通过将这些行添加到我的php代码中解决了这个问题:

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

why ? 为什么呢?

after debugging the code, it look like all the operations that i request are completed, so the only problem is that the request is pending, or lost its way back, somehow .. 调试代码后,看起来我请求的所有操作都已完成,所以唯一的问题是请求未决,或者以某种方式迷失了方向。

my best suggestion is because am using proc_open on my code .. 我最好的建议是因为我在代码上使用了proc_open ..

still not the best solution, but it is working and am half happy. 仍然不是最好的解决方案,但是它正在工作并且很高兴。

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

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