【问题标题】:Check what error value is in ajax callback检查ajax回调中的错误值
【发布时间】:2012-06-19 07:22:41
【问题描述】:

如何检查返回的错误值是 ajax 回调错误:

$.ajax({
            url: "myurl",       
            type: 'POST',
            dataType : "text",
            data : ({
                json : myjson
            }),
            success : function(data) {

            },
   error : function() {
                alert ('error');
    } 

        });  

【问题讨论】:

    标签: jquery


    【解决方案1】:

    尝试为 ajax 调用的错误部分访问这些参数:

    error: function (request, status, error) {
            alert(request.responseText);
    

    另一个例子:

    error: function(xhr) {
                    if(xhr.status == 422) {
                      alert(parseErrors(xhr));
                    } else {
                      alert('An error occurred while processing the request.');
                    }
    

    它们是 ajax 调用的一部分,你用逗号分隔它们。举个小例子:

    $.ajax({
      success: function(data) {
      },
      error: function(xhr) {
      }
      });
    

    更新: 基本上 xhr.status 是 http 状态号。

    alert("readyState: "+xhr.readyState);
    alert("status: "+xhr.status);
    alert("responseText: "+xhr.responseText);
    

    【讨论】:

    • 当错误被抛出时, xhr.status 的值为 0 。如果错误是有效的,它应该是别的东西吗?
    • 查看更新部分,以便您可以查看返回的错误消息中还有哪些内容,以便我们查看并将它们放在这里。
    【解决方案2】:

    类似question

    error : function(jqXHR, textStatus, errorThrown){
        alert(jqXHR.status);
    }
    

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

    【讨论】:

      猜你喜欢
      • 2015-03-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多