Getting AJAX response body for use in error callback

前端 未结 4 2108
鱼传尺愫
鱼传尺愫 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 01:12

    For a more recent and general answer (since jquery 1.5), I'd use the jqXHR object:

    $.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
        alert(jqXHR.responseText);
    })
    

    Alternatively responseJSON can be used to get the response body already parsed

    $.ajax(url).fail(function(jqXHR, textStatus, errorThrown) {
        console.log(jqXHR.responseJSON);
    })
    

提交回复
热议问题