Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
477 views
in Technique[技术] by (71.8m points)

jquery - jQuery Ajax错误处理,显示自定义异常消息(jQuery Ajax error handling, show custom exception messages)

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

(有没有什么方法可以在我的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");

(例如,如果我想通过扔在服务器端异常的Struts通过throw new ApplicationException("User name already exists");)

, I want to catch this message ('user name already exists') in the jQuery AJAX error message.

(,我想在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);
      }
    });
  }
});

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

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

I am not sure where I am going wrong.

(我不确定我哪里出错了。)

What can I do to fix this problem?

(我该怎么做才能解决这个问题?)

  ask by translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

(确保将Response.StatusCode设置为200以外的其他内容。使用Response.Write编写异常消息,然后使用...)

xhr.responseText

..in your javascript.

(..在你的JavaScript中。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...