MVC 3 Ajax.ActionLink 如何获取 onFailure 处理程序中的状态代码?

发布于 2024-10-10 02:20:24 字数 115 浏览 0 评论 0原文

在 Ajax 操作链接调用的操作中,我需要根据当前条件抛出 HttpException。根据这些条件,HttpException 具有不同的代码。

如何获取 onFailure 处理程序中的状态代码?

In the action called by an Ajax action link, I need to throw an HttpException based on the current conditions. Depending on those conditions, the HttpException has a different code.

How do I get the status code in the onFailure handler?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

难忘№最初的完美 2024-10-17 02:20:24

我遇到了同样的问题,我发现你不需要抛出 HttpException。相反:

return new HttpStatusCodeResult(401);

onFailure 处理程序应该如下所示:

function handleOnFailure(response) {
        alert(response.status);
    }

对我来说,现在唯一的问题是如何自己处理 401,而不是显示登录页面的 MVC。如果是 401,则不会显示警报,但对于任何其他状态代码,这都有效。

I had the same problem, and I figured out you do not need to throw an HttpException. Instead do:

return new HttpStatusCodeResult(401);

The onFailure handler should look like this:

function handleOnFailure(response) {
        alert(response.status);
    }

For me the only problem is now how to handle the 401 myself instead of MVC showing the logon page. In case of a 401 the alert is not shown, but with any other statuscode this works.

半暖夏伤 2024-10-17 02:20:24

只需将 AjaxOptions.OnFailure 属性设置为如下函数:

<script>
function onFailure(xhr, status) {
    alert(xhr.statusText);
}
</script>

请参阅这篇文章:https://www.experts-exchange.com/questions/28583187/MVC-Ajax-BeginForm-how-to-处理 OnSuccess-and-OnFailure-from-Controller.html

Simply set AjaxOptions.OnFailure property to a function like this:

<script>
function onFailure(xhr, status) {
    alert(xhr.statusText);
}
</script>

See this post: https://www.experts-exchange.com/questions/28583187/MVC-Ajax-BeginForm-how-to-handle-OnSuccess-and-OnFailure-from-Controller.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文