我需要显示 Json 返回的消息。
在 Controller 中,异常被抛出并被捕获在一个catch block 中。我正在返回故障错误消息。
在 Ajax 中,success 部分始终执行。但是如果是我的webservice的错误,我不想执行正常的;相反,我想显示一条错误消息。
我怎样才能做到这一点?
我的代码如下:
Controller
[HttpPost]
public JsonResult DeleteClientRecord()
{
bool result = true;
try
{
result = ClientCRUDCollection.DeleteClient(deleteClientId);
}
catch (Exception ex)
{
return Json(ex.Message, JsonRequestBehavior.AllowGet);
}
return Json(new { result }, JsonRequestBehavior.AllowGet);
}
AJAX 调用
$("#YesDelete").click(function () {
$.ajax({
type: "POST",
async: false,
url: "/Client/DeleteClientRecord",
dataType: "json",
error: function (request) {
alert(request.responseText);
event.preventDefault();
},
success: function (result) {
// if error from webservice I want to differentiate here somehow
$("#Update_" + id).parents("tr").remove();
$('#myClientDeleteContainer').dialog('close');
return false;
}
});
});
谁能帮我解决这个问题。
最佳答案
[HttpPost]
public JsonResult DeleteClientRecord()
{
bool result = true;
try
{
result = ClientCRUDCollection.DeleteClient(deleteClientId);
}
catch (Exception ex)
{
return Json(new { Success="False", responseText=ex.Message});
}
return Json(new { result }, JsonRequestBehavior.AllowGet);
}
关于javascript - 显示 JSON 返回的错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15006178/