【发布时间】:2017-08-31 23:07:44
【问题描述】:
我有一个 Ajax 方法,当我捕获控制器端异常时。我想将该异常传递给 Ajax 错误方法。 在这里它在异常触发后什么都不做。
Ajax 调用
function ConNews(){
$.ajax({
url: $("#getnewsdtl").val(),
type: "POST",
contentType: "application/json",
data: JSON.stringify(newsModelLists),
dataType: "json",
success: function (news) {
// some code here.
},
error: function (jqXHR, textStatus, errorThrown)
{
// I want get controller catched exception here.
}
});
控制器
[HttpPost]
public string GetNews(List<NewsModel> newsModelList)
{
try
{
return JsonConvert.SerializeObject(newsModelList);
}
catch (Exception ex)
{
return JsonConvert.SerializeObject("adf"); // I want to send thisone to Ajax error function
}
}
【问题讨论】:
-
在你的 catch 块中,你发回一个有效值,所以它永远不会命中
error块(只有在出现异常时才会被命中) -
如何将此值发送给xhr。有什么办法吗?
-
您的代码中没有任何意义,所以我不知道您真正想要做什么,但您可以在 catch 块中说
return new HttpStatusCodeResult(400, "your error message");(400 表示错误请求)
标签: jquery ajax asp.net-mvc asp.net-mvc-5