相关疑难解决方法(0)

jQuery Ajax错误处理,显示自定义异常消息

有没有什么方法可以在我的jQuery AJAX错误消息中显示自定义异常消息作为警报?

例如,如果我想通过Struts by 在服务器端抛出异常throw new ApplicationException("User name already exists");,我想在jQuery AJAX错误消息中捕获此消息('用户名已存在').

jQuery("#save").click(function () {
  if (jQuery('#form').jVal()) {
    jQuery.ajax({
      type: "POST",
      url: "saveuser.do",
      dataType: "html",
      data: "userId=" + encodeURIComponent(trim(document.forms[0].userId.value)),
      success: function (response) {
        jQuery("#usergrid").trigger("reloadGrid");
        clear();
        alert("Details saved successfully!!!");
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

在第二个警报,我警告抛出的错误,我得到undefined,状态代码是500.

我不确定我哪里出错了.我该怎么做才能解决这个问题?

ajax jquery custom-exceptions

708
推荐指数
15
解决办法
140万
查看次数

标签 统计

ajax ×1

custom-exceptions ×1

jquery ×1