jQuery的Ajax的错误处理,显示自定义异常消息 [英] jQuery Ajax error handling, show custom exception messages

查看:434
本文介绍了jQuery的Ajax的错误处理,显示自定义异常消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一些方法,我可以显示自定义异常消息,因为在我的jQuery的AJAX错误信息警告?

Is there some way I can show custom exception messages as an alert in my jQuery AJAX error message?

例如,如果我想通过扔在通过 Struts的服务器端异常抛出新ApplicationException的(用户名已经存在); ,我要抓住这个消息(用户名已经存在)的jQuery的AJAX错误信息

For example, if I want to throw an exception on the server side via Struts by throw new ApplicationException("User name already exists");, I want to catch this message ('user name already exists') in the jQuery AJAX error message.

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);
      }
    });
  }
});

在第二次警报,在这里我提醒抛出错误,我越来越未定义和状态code为500。

On the second alert, where I alert the thrown error, I am getting undefined and the status code is 500.

我不知道我要去的地方错了。我能做些什么来解决这个问题?

I am not sure where I am going wrong. What can I do to fix this problem?

推荐答案

请确保你设置 Response.Status code 来的东西比200写其他使用的Response.Write 的异常的信息,然后用...

Make sure you're setting Response.StatusCode to something other than 200. Write your exception's message using Response.Write, then use...

xhr.responseText

..在你的JavaScript。

..in your javascript.

这篇关于jQuery的Ajax的错误处理,显示自定义异常消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆