【问题标题】:MVC 3 Ajax.ActionLink How to get the status code in the onFailure handler?MVC 3 Ajax.ActionLink 如何在 onFailure 处理程序中获取状态码?
【发布时间】:2010-12-29 17:40:51
【问题描述】:

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

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

【问题讨论】:

    标签: asp.net-ajax ajax.net


    【解决方案1】:

    我遇到了同样的问题,我发现您不需要抛出 HttpException。而是这样做:

    return new HttpStatusCodeResult(401);
    

    onFailure 处理程序应如下所示:

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

    对我来说,现在唯一的问题是如何自己处理 401 而不是 MVC 显示登录页面。在 401 的情况下,警报不会显示,但任何其他状态码都可以。

    【讨论】:

    • @FabioMilheiro,当您抛出 HttpException 时,您总是会收到 500 内部服务器错误。
    • 好的。我记得我现在遇到了类似的问题。您正在使用 ActionLink。直接使用 jQuery。这是一个带有示例stackoverflow.com/questions/2064261/… 的答案。你应该达到你想要的结果(使用 xhr!)。
    • @FabioMilheiro:我更喜欢使用 MVC 助手来保持一切一致,但是(当我有空闲时间时..)我会尝试构建自己的助手。谢谢。
    【解决方案2】:

    只需将AjaxOptions.OnFailure 属性设置为这样的函数:

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

    看到这个帖子:https://www.experts-exchange.com/questions/28583187/MVC-Ajax-BeginForm-how-to-handle-OnSuccess-and-OnFailure-from-Controller.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-31
      • 2021-07-27
      • 2019-08-28
      • 1970-01-01
      • 2011-09-16
      • 2014-05-19
      相关资源
      最近更新 更多