代码之家  ›  专栏  ›  技术社区  ›  oshirowanen

描述性错误消息

  •  1
  • oshirowanen  · 技术社区  · 14 年前

    我有以下jQuery,它没有给出最具描述性的错误消息。。。

    url: 'server_page.aspx',
    type: 'POST',
    data: { intID:$(this).attr("id"), strState:"1" },
    error: function() { alert('Error'); },
    success: function() {  }
    

    如果可能的话,如何获得更多描述性错误消息?

    编辑:

    $(文档).ready(函数(){ var that=这个;

        if($(this).is(":checked")) { 
            $.ajax({
                url: 'favorite_on_off.aspx',
                type: 'POST',
                data: { strFavoriteID:$(that).attr("id"), strState:"1" },
                timeout: 1000,
                error: function(xhr, status, error)
                {
                    alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "1");
                    alert('Error: ' + status + '\nError Text: ' + error);  
                },
                success: function() {  }
            });
        } else {
            $.ajax({
                url: 'favorite_on_off.aspx',
                type: 'POST',
                data: { strFavoriteID:$(that).attr("id"), strState:"0" },
                timeout: 1000,
                error: function(xhr, status, error)
                {
                    alert("values: strFavoriteID: " + $(that).attr("id") + " strState: " + "0");
                    alert('Error: ' + status + '\nError Text: ' + error);  
                },
                success: function() {  }
            });
        }
    }); 
    

    以下是错误消息:

    值:strFavoriteID:c:\folder\文档.doc状态:1

    错误:错误 错误文本:未定义

    3 回复  |  直到 14 年前
        1
  •  3
  •   Nick Craver    14 年前

    可以使用传递给的所有参数 the error callback

    error: function(xhr, status, error) { 
      alert('Error: ' + status + '\nError Text: ' + error); 
    },
    
        2
  •  1
  •   lonesomeday    14 年前

    第二个参数提供给 error

    error: function(xhr, textStatus) { alert(textStatus); }
    

        3
  •  0
  •   rebelliard    14 年前

    我有这个方法:

    function HandleAjaxError(request, status, error) {
        var ex = eval("(" + request.responseText + ")");
    
     $('body').addClass("ui-widget-overlay");
        alert(ex.Message);
        $('body').removeClass("ui-widget-overlay");
    }
    
    $.ajax({
        type: "POST",
        url: window.location.pathname + "/DoStuff",
        data: "{}",
        success: Success,
        error: HandleAjaxError
    });
    

    这样,我就可以优雅地处理ASP.NET侧面。我通常记录原始错误,然后为客户机抛出一个自定义/描述性错误。