1

I have an ASP.NET MVC application, where I call method by AJAX:

        $.ajax({
            dataType: 'json',
            url: "/Admin/AllowVideoUpload",
            type: "POST",
            data: { val: val },
            error: function () {
                alert('Error');
            }
        });

if success - nothing happens on client side, if error - get 'Error' message. But error can be by different reasons - problem inside "AllowVideoUpload" method or user lost his credentials (AdminController has attribute 'Authorize(Roles = "Admin")'). I want to differ these 2 types of error. How to do it?

4

1 回答 1

0

这些是您的应用程序级别错误。所以它应该在成功处理程序中处理。

json您可以从具有状态/错误代码/消息元素的操作方法返回 ,您可以在其中提及错误原因。

错误的示例 Json

{
    "Status": "Failed",
    "Message": "Authentication Failed"
}

成功的示例 Json

{
    "Status": "Success",
    "Message": "Successfully Updated"
}

阅读 jSon,然后您可以决定下一步要做什么。可以向用户显示消息

$.ajax({
        dataType: 'json',
        url: "/Admin/AllowVideoUpload",
        type: "POST",
        data: { val: val },
        success:function(data){
           if(data.Status=="Success")
           {
               //operation success. do whatever
           }
           else if(data.Status=="Failed")
           {
             alert(data.Message);
           }
        },
        error: function () {
            alert('Error');
        }
});
于 2012-04-29T13:40:15.777 回答