【发布时间】:2011-07-10 07:56:34
【问题描述】:
我正在使用 asp.net mvc 构建一个 RESTful web api,它返回纯 json 数据。在我的客户端上,我使用backbone.js 与之通信。
我的问题是,如何在 javascript 中捕获消息?例如。如果用户无权删除或没有与 id 匹配的项目怎么办?我被告知要抛出 http 错误而不是自定义 json。 所以我的代码是:
[HttpDelete]
public ActionResult Index(int id)
{
if (id == 1)
{
throw new HttpException(404, "No user with that ID");
}
else if (id == 2)
{
throw new HttpException(401, "You have no authorization to delete this user");
}
return Json(true);
}
如何访问我的 javascript 回调中的消息?回调看起来像:
function (model, response) {
alert("failed");
//response.responseText would contain the html you would see for asp.net
}
在从服务器返回的数据中,我在任何地方都没有看到我在异常中抛出的消息。
【问题讨论】:
标签: javascript asp.net asp.net-mvc