0

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.

how can i check if the browser receive a 200 ok, then just abort the ajax call, and continue the jquery code.

Oussama Elgoumri
  • 609
  • 1
  • 5
  • 15

3 Answers3

1

You dont need to do that.

You can just use the success function in 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!

Ideal Bakija
  • 629
  • 5
  • 14
  • 2
    I was about to recommend the same, then I found this answer: http://stackoverflow.com/questions/8840257/jquery-ajax-handling-continue-responses-success-vs-done This says, that done is the preferred way. (Although I don't know why isn't it working) – fodma1 Dec 24 '14 at 13:58
  • i've used it, still the same, the browser does receive a 200 OK, the server side operation is completed successfuly, the data is updated, but the code inside success or done is not executed. – Oussama Elgoumri Dec 24 '14 at 13:59
  • the code is available on github, please any ideas can be helpful https://github.com/oussamaelgoumri/appRunner/blob/master/app/eo/src/js/app.js – Oussama Elgoumri Dec 24 '14 at 14:00
  • Check out in firebug how is the server responding to you. Are you printing your data after you update it on the server ?? – Ideal Bakija Dec 24 '14 at 14:25
  • @IdealBakija yes the server is responding, there is no data print, but all the php code is executed, please refer to the link in my previous comment to gain more insight about what am trying to do. – Oussama Elgoumri Dec 24 '14 at 14:36
  • Well if you are not printing the data on the server neither the done command nor the success command will execute. Just write 'echo true'(if your are using php) in the end of your server Code. This will force your ajax to call the above actions. Let me know if it helps you! – Ideal Bakija Dec 24 '14 at 15:23
  • @IdealBakija i've debugged the code, and i think the problem is because am using proc_open(), the test is working fine, the process is started, but the request is pending, take a look at the App.inc.php class, the method is called start() https://github.com/oussamaelgoumri/appRunner/blob/master/app/eo/App.inc.php – Oussama Elgoumri Dec 24 '14 at 16:36
  • @Oussama ELGOIMRI.. I have looked up at your code. I see you have in two places a 'return false'. That will stop the action and will not return an answer to ajax making it unable to call the Success or the Done action. Try to print 'echo true' instead of return false just to test if it will call the ajax. I am sorry I delayed the answer. I am a little busy once I find some free time I will try the code myself if you don't come up with a solution – Ideal Bakija Dec 26 '14 at 10:30
  • @IdealBakija did you see my answer above, i know its not correct, but at least it does the trick, i don't know why the ajax is not received once done, i intentialy redirected all the output to /dev/null, but yet, nothing ... please review the code, and tel me what you think – Oussama Elgoumri Dec 26 '14 at 15:31
0

Use dataType: 'json' in order to get server response

khaliq
  • 98
  • 9
0

i solved this problem by adding these lines to my php code:

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 ..

still not the best solution, but it is working and am half happy.

Oussama Elgoumri
  • 609
  • 1
  • 5
  • 15