Solved

Pass mesage from servlet to ajax in case of error

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

Expert Comment

by:Amitkumar Panchal
Comment Utility
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
Comment Utility
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 20

Accepted Solution

by:
Amitkumar Panchal earned 500 total points
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now