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

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

问题描述

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

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

例如,如果我想通过 Struts 在服务器端抛出异常,throw new ApplicationException("User name already exists");,我想在 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);
      }
    });
  }
});

在我提醒抛出的错误的第二个警报中,我收到 undefined 并且状态代码为 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.StatusCode 设置为 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天全站免登陆