Skip to main content
 首页 » 编程设计

jquery之发送至 UI 的异常消息

2026年07月28日9youxin

我打开一个 jQuery 对话框,在这个框中我执行保存/取消操作。为了保存,我调用我的 Controller ,进行一些验证,保存或抛出异常(MyPersonalException)。如果出现异常,我将返回另一个 View (“MessageError” View )以显示在弹出窗口中。我只想在模式框中看到“MyPersonalException”中可用的消息

我的问题: 1. 这是可行的,但仅适用于 Firefox,不适用于 IE,不适用于 Chrome 2.是否有其他方法,因为这看起来需要大量代码来显示消息。

Controller 看起来像这样:

[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult SaveOrUpdate(Guid id, string firstName, string LastName) 
{ 
    try 
    { 
        Employee employee = new Employee() { Id = id, FirstName = firstName, LastName = LastName }; 
        _employeeService.SaveOrUpdate(employee); 
        return Index(); 
    } 
    catch (MyPersonalException ex) 
    { 
        _model.ErrorMessage = ex.Message; 
        return View("MessageError", _model); 
 
    } 
    catch (Exception ex) 
    { 
        _model.ErrorMessage = ex.Message; 
        return View("MessageError", _model); 
    } 
} 

为了调用对话框,我使用这段代码

jQuery(文档).ready(function() { $(函数() { /* 变量名称 = $("#firstName"), 电子邮件 = $("#lastName"), 密码 = $("#isActive"), allFields = $([]).add(姓名).add(电子邮件).add(密码), 提示 = $("#validateTips");*/

    $("#dialog").dialog({ 
        bgiframe: true, 
        autoOpen: false, 
        modal: true, 
        buttons: { 
            Save: function() { 
                $.ajax({ 
                    type: "POST", 
                    url: "/Employee/SaveOrUpdate", 
                    data: { 
                        id: getId(), 
                        firstName: getFirstName(), 
                        lastName: getLastName() 
                    }, 
                    success: function(data) { 
                        if (jqueryShowResult(data)) 
                            $("#DisplayError").html(data); 
                        else { 
                            employeeId = 0; 
                            $(this).dialog('close');                                
                        } 
                    }, 
                    error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    } 
                }) 
 
            }, 
            Cancel: function() { 
                employeeId = 0; 
                $(this).dialog('close'); 
            } 
        }, 
        close: function() { 
            $("#gridEmpoyee").trigger("reloadGrid"); 
        }, 
        open: function() { 
            $.ajax({ 
                type: "POST", 
                url: "/Employee/GetEmployee", 
                data: { 
                    id: employeeId 
                }, 
                success: function(data) { 
                    $("#employeeDetail").html(data); 
                }, 
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                } 
            }) 
        } 
    }); 
}); 

});

jQueryShowResult

<script type="text/javascript"> 
    jqueryShowResult = function(msg) { 
        var browser; 
        try //Internet Explorer 
                    { 
            xmlDocTest = new ActiveXObject("Microsoft.XMLDOM"); 
            browser = "IE"; 
        } 
        catch (e) { 
            browser = "FF"; 
        } 
 
        if (browser == "IE") { 
            try { 
                xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); 
                xmlDoc.async = "false"; 
                xmlDoc.loadXML(msg); 
                var message = xmlDoc.getElementsByTagName("message")[0].childNodes[0].nodeValue; 
                var code = xmlDoc.getElementsByTagName("code")[0].childNodes[0].nodeValue; 
 
                return false; 
            } 
            catch (e) { 
                return true; 
            } 
        } 
        else { 
 
            var code = $(msg).find('code').text(); 
            var message = $(msg).find('message').text(); 
            if (code == "500") { 
                return false; 
            } 
            else { 
                return true; 
            } 
        } 
    };  
 </script> 

请您参考如下方法:

更新:抱歉,我们使用自定义包装器。 jQuery 默认情况下成功时不包含 xmlHttpRequest。下面是另一种方法,这种方法完全不需要改变您的观点。您基本上只是检查响应中带有 id='code' 的元素,如果存在,则显示错误。

success: function(data, textStatus) { 
  if ($("#code",data).length) { //See if the element <whatever id='code'> exists 
    $("#DisplayError").html(data); 
  } else { 
    employeeId = 0; 
    $(this).dialog('close');                                
  } 
}, 

这是 jQuery 1.4 版本(请参阅 changes here,请注意成功回调接收 XHR 对象作为第三个参数):

首先,在 View 中将 StatusCode 设置为 210 context.HttpContext.Response.StatusCode = 210;,然后使用以下回调格式:

success: function(data, textStatus, xhr) { 
  if (xhr.status == 210) { 
    $("#DisplayError").html(data); 
  } else { 
    employeeId = 0; 
    $(this).dialog('close');                                
  } 
},