2010-11-18 11 views
5

Denetleyici: Ürünler ve İşlem: Kaydetme, bir JsonResult döndürün. Kapanmış bir özel durum oluşursa, bu hatayı istemciye (örn: jQuery) özel bir hata iletisi ile bildirmek isterim. Bunu hem sunucuda hem de istemcide nasıl yapabilirim? Bu senaryoda işlev işaretçisi hatası kullanabilir miyim? İşte ASP.NET MVC: Bir AJAX isteğinin özel bir hata iletisiyle başarısız olduğunu jQuery'ye gönderme

istemci kodu

$.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 

       } 
      }); 

cevap

5

Eğer istek (başarıyla tamamlanmadı Denetleyiciniz eylemi anlamına başarısız olduğunda çağrılır bakın error işlevi var; kullanıcı yaptığında örneğin, IIS aşağı oldu talep). Bkz http://api.jquery.com/jQuery.ajax/. Denetleyiciniz İşlem başarıyla temas ve istemci şey hatalı olduğunu Denetleyiciniz eylem içine ne olduğunu bildirmek istiyoruz ise

, istemci tarafı JS anlayacak bir Error veya ErrorCode özelliği içeren bir JsonResult dönmelidir.

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; 
} 

Ve aşağıdaki JavaScript o hatayı ayrıştırmak yazardı:

Örneğin, kontrolör eylem şuna benzer olabilir yardımcı olur

$.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 
     } 
    } 
}); 

Umut.

:
0

hatası yakalamak ve geri düz metin olarak gönderilen alır sağlamak, hem de jQuery bilmesi için bir sorun ve hata fonksiyonu çalışacaktır oldu (500 Hata kodu ayarlarına bir clientError niteliği kullanılır
/// <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; 
    } 
} 
İlgili konular