【发布时间】:2015-11-06 10:43:23
【问题描述】:
在 ASP MVC 中,我有一个返回 json 数据的控制器:
public JsonResult Edit(int? id)
{
if(id==null)
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { message = "Bad Request" }, JsonRequestBehavior.AllowGet);
}
car carTmp = db.car.Find(id);
if (carTmp == null)
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return Json(new { message = "Not Found" }, JsonRequestBehavior.AllowGet);
}
return Json(carTmp, JsonRequestBehavior.AllowGet);
}
我也有以下ajax请求:
$.getJSON("Edit/" + data, function (result) {
var car = result;
})
.error(function (error) {
alert(error.message);
})
为什么,在成功的情况下,在结果对象中我有 json 对象(即:我可以访问 result.id、result.name ecc...)但在错误的情况下,error.message 是未定义的? (我已经将消息放入error.responseJson.message)
【问题讨论】:
-
因为结果即将到来,我的意思是在 null 的情况下,您将返回一条错误消息,但在成功的情况下也是如此。您可以通过向模型发送 null 来验证它一次
标签: c# asp.net .net ajax asp.net-mvc