Solved

Pass mesage from servlet to ajax in case of error

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
netstat -ano | find "8000" and taskkill /f /pid 2984 3 56
login jsp example 24 48
spring jars download 1 27
Is Applet the way to go for my drag and drop system? 8 11
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

770 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