MVC ajax调用如何处理错误响应

时间:2022-11-29 12:29:30

I have a question regarding the ajax call: here is my ajax call:

我有一个关于ajax调用的问题:这是我的ajax调用:

$.ajax({
    url: "/Article/DeleteArticle/"+id,
    type: "GET",
    error: function (response) {

    },
    success: function (response) {

    }
});

And here is my controller:

这是我的控制器:

public ActionResult DeletePicture(int id)
{
    bool success = Operations.DeleteArticle(id);
    return null;
}

I would like to know what shoulud i return to get inside error? And when is this error function is called basically? If error happens on server or ..?

我想知道我回到哪里以获得内部错误?什么时候这个错误函数基本被调用?如果服务器上发生错误或..?

And regarding the success how can i pass there some data?

关于成功我怎样才能传递一些数据?

Real life example:

真人生活的例子:

Imagine i call this ajax method to delete an article, when it is deleted, so success i would like to show some success message. If it failed so in my action i get success=false, i would like to show some other message, like : failed.

想象一下,我把这个ajax方法称为删除文章,当它被删除时,所以我想成功地展示一些成功的消息。如果它失败了,那么在我的行动中我得到成功=假,我想展示一些其他消息,如:失败。

How to achieve that?

怎么实现呢?

2 个解决方案

#1


4  

You can handle your Ajax calls by creating an object that represents the response:

您可以通过创建表示响应的对象来处理Ajax调用:

public class AjaxResponse
{
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}

Then return it as follows:

然后按如下方式返回:

public ActionResult DeletePicture(int id)
{
    // success failed by default
    var response = new AjaxResponse { Success = false };
    try 
    {
     bool success = Operations.DeleteArticle(id);
     response.Success = success;
     // Set a message for UI
     response.Message = success ? "Success" : "Failed";
     }
     catch
     {
      // handle exception
      // return the response with success false
      return Json(response, JsonRequestBehavior.AllowGet);
     }
     return Json(response, JsonRequestBehavior.AllowGet);
}

You could then pass the data and handle it as follows:

然后,您可以传递数据并按如下方式处理:

$.ajax({
    url: "/Article/DeleteArticle/",
    type: "GET",
    data : { Id : id },
    dataType: 'json',
    error: function (response) {

    // Handle error from response.Success or response.Message

    },
    success: function (response) {

        // Handle error from response.Success or response.Message

    }
});

The handle error could simply display the message back to an HTML element or popup some kind of javascript notification.

句柄错误可以简单地将消息显示回HTML元素或弹出某种javascript通知。

#2


1  

you can use it

你可以使用它

public ActionResult DeleteArticle(int id)
{
    bool success = Operations.DeleteArticle(id);       

    return Json(success, JsonRequestBehavior.AllowGet);
}


$.ajax({
    url: "/Article/DeleteArticle/",
    type: "GET",
    data : { Id : id },
    dataType: 'json',
    error: function (response) {    
       if(response!=null && response.length!=0)
       {
         alert('error');
       }    
    },
    success: function (response) {  
       if(response) {
         alert('Success');
       }   
    }
});

#1


4  

You can handle your Ajax calls by creating an object that represents the response:

您可以通过创建表示响应的对象来处理Ajax调用:

public class AjaxResponse
{
        public bool Success { get; set; }
        public string Message { get; set; }
    }
}

Then return it as follows:

然后按如下方式返回:

public ActionResult DeletePicture(int id)
{
    // success failed by default
    var response = new AjaxResponse { Success = false };
    try 
    {
     bool success = Operations.DeleteArticle(id);
     response.Success = success;
     // Set a message for UI
     response.Message = success ? "Success" : "Failed";
     }
     catch
     {
      // handle exception
      // return the response with success false
      return Json(response, JsonRequestBehavior.AllowGet);
     }
     return Json(response, JsonRequestBehavior.AllowGet);
}

You could then pass the data and handle it as follows:

然后,您可以传递数据并按如下方式处理:

$.ajax({
    url: "/Article/DeleteArticle/",
    type: "GET",
    data : { Id : id },
    dataType: 'json',
    error: function (response) {

    // Handle error from response.Success or response.Message

    },
    success: function (response) {

        // Handle error from response.Success or response.Message

    }
});

The handle error could simply display the message back to an HTML element or popup some kind of javascript notification.

句柄错误可以简单地将消息显示回HTML元素或弹出某种javascript通知。

#2


1  

you can use it

你可以使用它

public ActionResult DeleteArticle(int id)
{
    bool success = Operations.DeleteArticle(id);       

    return Json(success, JsonRequestBehavior.AllowGet);
}


$.ajax({
    url: "/Article/DeleteArticle/",
    type: "GET",
    data : { Id : id },
    dataType: 'json',
    error: function (response) {    
       if(response!=null && response.length!=0)
       {
         alert('error');
       }    
    },
    success: function (response) {  
       if(response) {
         alert('Success');
       }   
    }
});