szp*_*pic 5 ajax asp.net-mvc json
你好我有一个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)
}
}
})
});
Run Code Online (Sandbox Code Playgroud)
这是控制器方法:
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 });
}
Run Code Online (Sandbox Code Playgroud)
但我总是得到内部500错误.通过方法接收数据,所有计算都可以.我在例子中编写了返回JSON.我犯了哪个错误?
默认情况下,ASP.NET MVC拒绝ajax GET请求,您必须通过显式设置JsonRequestBehavior来允许它 AllowGet:
return Json(new { ok = true, quantity = result.Quantity},
JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11339 次 |
| 最近记录: |