这是我的场景,我从数据库中获取数据,在 Controller 操作中尝试/捕获它,并希望将代码和错误消息传递回我的js。
//in my action
[HttpPost]
public JsonResult GetWorkorderDetails(string folderno)
{
try{
//do some stuff - fetch the data
}catch() {
//if error, I would like to set the JsonResult to return bad status and error message
}
}
//in my js
if(response.status == 400){
//if error, output the message response.responseText and return
}
有什么办法可以在catch语句中将我的响应状态设置为400?
最佳答案
我将返回类型更改为通用ActionResult,并在需要时返回新的HttpStatusCodeResult。
关于json - 如何获得MVC Action 以返回错误状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26679239/