2014-05-23 20 views
5
kullanırken 500 (Dahili Sunucu Hatası) durumuyla cevap

Merhaba ben bir ajax aramanız var:Sunucu Ajax

$.ajax({ 
     url: "/Orders/CheckIfExists", 
     type: "GET", 
     contentType: "application/json; charset=utf-8", 
     data: { 
      catalogNumber: viewModel.catalogNumber, 
      quantity: viewModel.quantity 
     }, 
     error: function (data) { 
      alert("wystąpił nieokreślony błąd " + data); 
     }, 
     success: function (data) { 
      if(data.ok) 
      { 
       alert(data.quantity) 
      } 
     } 
    }) 
}); 

ve burada kontrolör bir yöntem vardır:

public JsonResult CheckIfExists(string catalogNumber, int quantity) 
    { 
     List<Expression<Func<DeviceInstance, bool>>> where = new List<Expression<Func<DeviceInstance, bool>>>(); 
     where.Add(w=>w.DeviceUsage.UserId==1); 
     where.Add(w => w.Project == null); 
     where.Add(w => w.Device.CatalogNo == catalogNumber); 
     var result = unitOfWork.deviceInstanceRepository.Get(where) 
      .GroupBy(w => new 
      { 
       DeviceId = w.DeviceId, 
       CatalogName = w.Device.CatalogNo, 
      }) 
      .Select(s => new 
      { 
       Quantity = s.Sum(x => x.Quantity), 
      }).First(); 
     if (result.Quantity >= quantity) 
     { 
      return Json(new { ok = true, quantity = result.Quantity}); 

     } 
     return Json(new { ok = false }); 
    } 

Ama her zaman alıyorum Dahili 500 hatası. Veriler yöntemle alınır ve tüm hesaplamalar tamamdır. Örnek olarak JSON'u iade ediyorum. Nerede hata yaptım? Varsayılan ASP.NET MVC ile

cevap

6

ajax GET isteklerini reddeder, açıkça AllowGet için JsonRequestBehavior ayarlayarak buna izin zorunda:

return Json(new { ok = true, quantity = result.Quantity}, 
    JsonRequestBehavior.AllowGet); 
+0

Teşekkür adam !!! ... Seni seviyorum –