jQuery Ajax error handling show custom exception messages

0 votes

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

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

On the second alert in the error callback, where I alert thrownError, I am getting undefined and the xhr.status code is 500.

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

Jun 2 in JQuery by Edureka
• 13,640 points
39 views

1 answer to this question.

0 votes

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

xhr.responseText

..in your javascript.

answered Jun 2 by Edureka
• 13,640 points

Related Questions In JQuery

0 votes
1 answer

Show Page Loading Spinner on Ajax Call in jQuery Mobile

 Use the ajaxStart() and ajaxStop() Method While working ...READ MORE

answered Jun 10 in JQuery by gaurav
• 13,460 points
21 views
0 votes
1 answer

Uncaught Error: Bootstrap's JavaScript requires jQuery

Hello @kartik, You have provided wrong order for ...READ MORE

answered Apr 28, 2020 in JQuery by Niroj
• 82,720 points
15,210 views
0 votes
1 answer

Error: no such method 'show' for tooltip widget instance

Hello, The root of this and similar problem "no such method ...READ MORE

answered May 12, 2020 in JQuery by Niroj
• 82,720 points
1,718 views
0 votes
1 answer

Error:jQuery scrollTop not working in Chrome but working in Firefox

Hello @kartik, If your CSS html element has the following overflow markup, scrollTop will ...READ MORE

answered May 29, 2020 in JQuery by Niroj
• 82,720 points
6,486 views
0 votes
1 answer

jQuery AJAX submit form

There is a simple input mentioned below ...READ MORE

answered Feb 8 in Java by Soham
• 8,730 points
174 views
0 votes
1 answer

TypeError: $.ajax(...) is not a function?

Please double-check if you're using the full-version ...READ MORE

answered Feb 22 in Others by Aditya
• 7,280 points
177 views
0 votes
0 answers

Edit PHP query code depending on image map clicked area using AJAX

I am working on an image map ...READ MORE

Jun 3 in PHP by Kichu
• 19,040 points
11 views
0 votes
0 answers

How can I upload files asynchronously with jQuery?

I would like to upload a file ...READ MORE

Jun 10 in JQuery by gaurav
• 13,460 points
17 views
0 votes
1 answer

JQuery Difference between hide() and fadeOut() , show() and fadeIn()

Main difference between FadeIn, FadeOut vs hide, ...READ MORE

answered Jun 1 in JQuery by Edureka
• 13,640 points
3 views
0 votes
1 answer

jQuery fix for "Uncaught TypeError: $ is not a function" error

We can fix this error by using jQuery() . ...READ MORE

answered Jun 1 in JQuery by Edureka
• 13,640 points
43 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP