【问题标题】:Razor MVC 5.2.3 how to call OnFailure with ajax postRazor MVC 5.2.3 如何使用 ajax post 调用 OnFailure
【发布时间】:2016-05-18 20:13:26
【问题描述】:

我有一个 ajax 表单,我想在其中调用两个不同的 javascript 函数,一个代表成功,一个代表失败。在这两个电话中,我pass data back from the server to the javascript。请注意以下几点:

我有这个想法

@using (Ajax.BeginForm("MyAction", null,
new AjaxOptions
{
    HttpMethod = "POST", // HttpPost
    InsertionMode = InsertionMode.Replace, 
    UpdateTargetId = "myDiv", 
    OnSuccess = "callSuccess(data.msg)",
    OnFailure = "callFailure(data.msg)",
}, new { @class = "form-inline" }))
{
   ... // my content
}

我的控制器在返回帖子时具有以下逻辑。

    public ActionResult MyAction(string myString)
    {
        ...
        // If error occurred
        return Json(new { success = false, msg= "Fail" });
        ...
        // If no errors
        return Json(new { success = true, msg= "Success" });
    }

无论返回什么,OnSuccess 都是唯一被调用的。拨打OnFailure的正确方法是什么?

【问题讨论】:

  • 您返回的有效数据(不抛出异常或返回错误代码),因此它将始终转到成功函数。
  • "success" 表示 ajax 调用成功 - 即 http 请求/接收过程完成且没有 http 错误。 AJAX 不知道您将“成功”属性设置为 false。

标签: javascript jquery ajax asp.net-mvc razor


【解决方案1】:

考虑使用 HTTP 状态码

您可以考虑通过返回HttpStatusCodeResult 对象来返回与您要触发的错误类型(即未授权、错误请求等)相对应的适当HTTP status code

// If an error occurred...
if(error)
{
     // Indicate your code and error message. This uses the Bad Request status (400) 
     return new HttpStatusCodeResult(400, "Something went wrong...");
}

处理这个客户端

如果您不想这样做,您可以考虑定义一个结果事件(即OnSuccess = "call(data);),然后在该事件中,检查您的success 属性以确定要做什么:

function call(data){
      if(data.success){
          callSuccess(data.msg);
      }
      else{
          callFailure(data.msg);
      }
}

【讨论】:

    【解决方案2】:

    在 Rion Williams 帖子的帮助下,我能够调用这两个函数。更重要的是,我弄清楚了如何在成功/失败时访问和显示所需的消息:

    我的控制器

    public ActionResult MyAction(string myString)
    {
        ...
        // If error occurred
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "Error occurred!");
        ...
        // If no errors
        return Json(new { success = true, msg= "Success" });
    }
    

    我的看法:

    @using (Ajax.BeginForm("MyAction", null,
    new AjaxOptions
    {
        HttpMethod = "POST", // HttpPost
        InsertionMode = InsertionMode.Replace, 
        UpdateTargetId = "myDiv", 
        OnSuccess = "callSuccess(data.msg)",
        OnFailure = "callFailure(error)",    // error = "Error occurred!"
    }, new { @class = "form-inline" }))
    {
       ... // my content
    }
    

    【讨论】:

    • 这将不再适用于 HTTP/2,如下所述:stackoverflow.com/questions/46922043/…
    • 这也取决于“如果错误”部分出了什么问题。这真的是一个糟糕的要求吗?或者是否有其他问题(例如磁盘已满不是“错误请求”)。使用正确的 HttpStatusCode。
    【解决方案3】:

    是的,它总是会调用 OnSuccess。 但是您可以在 onSuccess 函数中检查您的成功属性。

        OnSuccess : function (data)
     {                     
        if(success)
        {
        alert('Json Return Success')
        }
        else
        {
        alert('json Result False')
        }
    
    }
    

    否则你必须发送 Rion Williams 提到的错误状态

    【讨论】:

    • 如果是这种情况,OnFailure Ajax 选项是否有目的?还是通过返回 HttpStatusCodeResult 触发?
    • 如果在运行时出现任何错误,那么它会转到 OnFailure 而不是 onSuccess。您也可以像这样添加错误.. ModelState.AddModelError("", "Enter all the values");。我希望它去 onFailure
    猜你喜欢
    • 2021-04-02
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-13
    相关资源
    最近更新 更多