Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Pass mesage from servlet to ajax in case of error

Posted on 2014-03-08
3
Medium Priority
?
1,079 Views
Last Modified: 2014-03-18
hi,

I am using ajax to insert a record in database through Servlet.
Record get inserted properly.
the problem is how to send the exception messages in the ajax method so that appropriate  can be displayed to user in case exception occurs?
             
$.ajax({
                         type: 'post',
                         url: 'registdata',
                         data: $('#registdataform').serialize(),
                         success: function (data) {
                              // $("#form")[0].reset();
                               alert("sucess");
                         },
                         error: function(data) {
                             // check status && error

                             
                          },
                             
                         });

how to pass message through servlet in  ajax?
0
Comment
Question by:Rocking
  • 2
3 Comments
 
LVL 21

Expert Comment

by:Amitkumar Panchal
ID: 39915235
You can send the response with error code as well as error message and display it in the alert box.

For example:
Message code 0 (Successful)
Message code 1 (Error 1)
Message code 2 (Error 2)
and so on
0
 

Author Comment

by:Rocking
ID: 39915396
i have set the response.setStatus(500); but how to get the status on the ajax call?
An exception occurs at server side and i set the above status manually then how to retrieve in ajax call i.e in data or in success?

Because in one type of exception it goes in error and for another type exception it goes in success block? I am unable to understand why is it so?
0
 
LVL 21

Accepted Solution

by:
Amitkumar Panchal earned 1500 total points
ID: 39916015
In any case, you should sent response with success status only. But, the response should contain at least two values: 1. result code, 2. result message and 3. data (optional).

This is how we should drive. Don't jump into various code of servlet api. Ideally, we should use it, but it make GUI more complicated.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

886 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question