Getting AJAX response body for use in error callback

前端 未结 4 2106
鱼传尺愫
鱼传尺愫 2020-12-10 00:08

jQuery\'s AJAX error function has the following parameters:

error(XMLHttpRequest, textStatus, errorThrown)

What\'s the best cross-browser w

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-10 00:53

    One straightforward usage example with jQuery:

    var url = '/';
    $.get(url).then(
      function(response) {
          $("#result").html(response);
      },
      function(jqXHR) {
        $("#result").html('Error occurred: '+ jqXHR.statusText + ' ' + jqXHR.status);
      }
    ); 
    

    This should return the HTML of current website front page.

    Then try entering a nonsense URL that doesn't exist and see the error thrown. You should get "404 Not Found" from web server. Test it: JSFiddle here

提交回复
热议问题