2014-05-23 12 views
5

Ciao Ho un chiamata AJAX:server ha risposto con uno stato di 500 (Internal Server Error) quando si utilizza 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) 
      } 
     } 
    }) 
}); 

ed ecco il metodo di controllo:

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

Ma sto ottenendo sempre Errore interno 500. I dati sono ricevuti con il metodo e tutti i calcoli sono ok. Compongo restituire JSON come nell'esempio. Dove ho fatto un errore?

risposta

6

Di default ASP.NET MVC respinge ajax GET richieste, è necessario consentire impostando esplicitamente JsonRequestBehavior-AllowGet:

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

Grazie uomo !!! ... Ti amo –

Problemi correlati