你好我有一个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) } } }) });
这是控制器方法:
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 }); }
解决方法
默认情况下,ASP.NET MVC拒绝ajax GET请求,您必须通过将
JsonRequestBehavior明确设置为AllowGet来允许它:
return Json(new { ok = true,quantity = result.Quantity},JsonRequestBehavior.AllowGet);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。