2010-11-18 32 views
5

Bộ điều khiển: Sản phẩm và Hành động: Lưu, trả về một JsonResult. Nếu một ngoại lệ bị mắc kẹt xảy ra, tôi muốn báo hiệu lỗi đó cho máy khách (ví dụ: jQuery) với một thông báo lỗi tùy chỉnh. Làm thế nào tôi có thể làm điều đó cả trên máy chủ và máy khách? Tôi có thể sử dụng lỗi con trỏ hàm trong trường hợp này không?ASP.NET MVC: báo hiệu cho jQuery rằng một yêu cầu AJAX đã thất bại với thông báo lỗi tùy chỉnh

Đây là mã khách hàng

$.ajax({ 
       url: '/Products/Save', 
       type: 'POST', 
       dataType: 'json', 
       data: ProductJson, 
       contentType: 'application/json; charset=utf-8', 
       error: function() 
       { 
        //Display some custom error message that was generated from the server 
       }, 
       success: function (data) { 
        // Product was saved! Yay 

       } 
      }); 

Trả lời

5

Các error chức năng mà bạn tham khảo được gọi khi yêu cầu thất bại (có nghĩa là hành động điều khiển của bạn không hoàn thành công, ví dụ, IIS đã xuống khi người dùng làm yêu cầu). Xem http://api.jquery.com/jQuery.ajax/.

Nếu hành động điều khiển của bạn được tiếp xúc với thành công và bạn muốn cho khách hàng biết rằng cái gì đã xảy ra bên trong hành động điều khiển của bạn đó là sai lầm, bạn nên trả về một JsonResult có chứa một tài sản mà bên khách hàng của bạn JS sẽ hiểu Error hoặc ErrorCode.

Ví dụ, hành động điều khiển của bạn có thể trông giống như thế này:

public ActionResult Save() 
{ 
    ActionResult result; 
    try 
    { 
     // An error occurs 
    } 
    catch(Exception) 
    { 
     result = new JsonResult() 
     { 
     // Probably include a more detailed error message. 
     Data = new { Error = true, ErrorMessage = "Product could not be saved." } 
     }; 
    } 
    return result; 
} 

Và bạn sẽ viết mã JavaScript sau đây để phân tích lỗi rằng:

$.ajax({ 
    url: '/Products/Save', 
    'POST', 
    'json', 
    ProductJson, 
    'application/json; charset=utf-8', 
    error: function() 
    { 
     //Display some custom error message that was generated from the server 
    }, 
    success: function (data) { 
     if (data.Error) { 
     window.alert(data.ErrorMessage); 
     } 
     else { 
     // Product was saved! Yay 
     } 
    } 
}); 

Hy vọng rằng sẽ giúp.

0

tôi đã sử dụng một thuộc tính clientError để bắt lỗi và đảm bảo nó được đưa trở lại dưới dạng văn bản thuần tuý, cũng như thiết lập các mã lỗi 500 (vì vậy jQuery biết có một vấn đề và hàm lỗi sẽ chạy:

/// <summary>Catches an Exception and returns just the message as plain text - to avoid full Html 
/// messages on the client side.</summary> 
public class ClientErrorAttribute : FilterAttribute, IExceptionFilter 
{ 
    public void OnException(ExceptionContext filterContext) 
    { 
     var response = filterContext.RequestContext.HttpContext.Response; 
     response.Write(filterContext.Exception.Message); 
     response.ContentType = MediaTypeNames.Text.Plain; 
     response.StatusCode = (int)HttpStatusCode.InternalServerError; 
     response.StatusDescription = filterContext.Exception.Message; 
     filterContext.ExceptionHandled = true; 
    } 
} 
Các vấn đề liên quan