Solved

Pass mesage from servlet to ajax in case of error

Posted on 2014-03-08
3
992 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ejb example issues 3 89
Wordpress Query 5 60
Java class and jar 3 61
Content of a read only textbox are not being saved in table in asp.net - VB 5 36
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…
Suggested Courses

751 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