$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - What is the best way to check if Ajax call is 200 OK if I'm returning the table? - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - What is the best way to check if Ajax call is 200 OK if I'm returning the table? - Stack Overflow

matteradmin19PV0评论

I have a question about ajax success. So in previous situations I have returned Data something like ID but now in this case I'm returning entire table. So I would like to check if Ajax was successful(200 OK) before I retrieve my data. What is the best way to do that? Also I use new way to retrieve data and check for errors with JQuery. Here is example of my code:

<div id="box"></div>

function getSlots(fld){
    var userID = '134';

    $j.ajax({
        type:  'POST',
        url:  'AjaxData.html',
        cache:  false,
        data:  {'userID':userID},
            dataType: "html",
            async:   false
        })  
        .done(function(html){
            $j('#box').html(html);
        })
        .fail(function(jqXHR, textStatus, errorThrown){ 
            gwLogIt(errorThrown);
        }); 
    }

I have a question about ajax success. So in previous situations I have returned Data something like ID but now in this case I'm returning entire table. So I would like to check if Ajax was successful(200 OK) before I retrieve my data. What is the best way to do that? Also I use new way to retrieve data and check for errors with JQuery. Here is example of my code:

<div id="box"></div>

function getSlots(fld){
    var userID = '134';

    $j.ajax({
        type:  'POST',
        url:  'AjaxData.html',
        cache:  false,
        data:  {'userID':userID},
            dataType: "html",
            async:   false
        })  
        .done(function(html){
            $j('#box').html(html);
        })
        .fail(function(jqXHR, textStatus, errorThrown){ 
            gwLogIt(errorThrown);
        }); 
    }
Share Improve this question asked Apr 29, 2016 at 19:05 espresso_coffeeespresso_coffee 6,12012 gold badges96 silver badges220 bronze badges 7
  • Why do you need to check for 200 OK? Doesn't .done check for that for you? What problem are you facing with this code? – gen_Eric Commented Apr 29, 2016 at 19:06
  • P.S. Why are you using async: false? I wouldn't suggest using that because it will lock up the user's browser until the call is done. – gen_Eric Commented Apr 29, 2016 at 19:07
  • I do not know if .done actually check for that. In the past I was putting if statement inside of success. – espresso_coffee Commented Apr 29, 2016 at 19:07
  • What would you remend to use instead? – espresso_coffee Commented Apr 29, 2016 at 19:08
  • Instead of async: false? Or I do not need that. – espresso_coffee Commented Apr 29, 2016 at 19:15
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You don't need to check. The various callbacks are called under the following conditions:

  • .done() or success: are called when the AJAX call is successful.
  • .fail() or error: are called when there's an error (either from the server or in the jQuery code that parses the response).
  • .always() or plete: are called in either case.

from official docs:

$.ajax({
  statusCode: {
    404: function() {
      alert( "page not found" );
    }
  }
});

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

also if you use something like babel to transpile ES6, you can use fetch api for ajax calls. I personally stopped using jQuery ±2 years ago.. when moved to React world :)

Post a comment

comment list (0)

  1. No comments so far