Solved

Pass mesage from servlet to ajax in case of error

Posted on 2014-03-08
3
971 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How can i  prevent default  Submision? 6 37
Notify sent to other threads in Java 9 41
Java 8 to Java 6 8 32
junit as external jar or library 7 37
Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

685 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