jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Need to perform an insert operation with AJAX Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Need to perform an insert operation with AJAX" Watch "Need to perform an insert operation with AJAX" New topic
Author

Need to perform an insert operation with AJAX

PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Hello please help

I have a textarea on to the jsp page where user can enter a comment , i am using AJAX to submit the data .

I am taking this data inside send method of the XMLHttpRequest.




My question is that , after performing the operation , how can i inform the status of the insert to the user .


Please guide me with your valuable suggestions.



K West
Ranch Hand

Joined: Sep 10, 2008
Posts: 58

When the property readyState is 4 that means the response is complete and we can get our data.



Years teach us more than books.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Hello West ,

In the DAO implementation class , i am using



This may either insert successfully or may also , so how do i capture the status ??
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You need to return something back in the Ajax call that says if it was a success or failure. It is up to you how you return it back and get the result as K West pointed out.

Eric
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
hey Eric ,

This was the response i was looking for till now .

As you said " It is up to you how you return it back and get the result"

what do you suggest me to do here ?

Can you please give me an idea how to construct a return type and depend on what conditions .
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

Returning a JSON response with data seems to be called for.

That data can be as simple as true or false (success or failure), or a complex construct that contains severities and messages. Depends upon your needs.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
I am planning to implement in this way :

Declare a string str ;

If an insert fails under the catch block



Am i going in a right way ?
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Bear Bibeault wrote:Returning a JSON response with data seems to be called for.

That data can be as simple as true or false (success or failure), or a complex construct that contains severities and messages. Depends upon your needs.


Can you please point to a link or give an idea of how to achieve this ?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
JSON is a good place to start

Eric
K West
Ranch Hand

Joined: Sep 10, 2008
Posts: 58



Is this in your jsp?

If so, you have to just send this back to ajax call by



or


at end of your jsp page.

And then get it in ajax response



And then check weather the resp is sucess or error.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Eric Pascarello wrote:JSON is a good place to start

Eric


JSON inside DAO implementation class ??
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60804
    
  65

PavanPL KalyanK wrote:JSON inside DAO implementation class ??

If your DAOs are in the business of generating a response, then you are definitely doing it wrong.

When a business operation returns its result status, it is the job of the presentation layer to create a response. That response can be a JSON-formatted construct that's easy to consume (much easier than XML) in JavaScript.

If you are doing Ajax "by hand", you're just making things hard on yourself. You should be using a library such as jQuery (or other of your choice) which makes Ajax, and evaluating JSON responses, easy as pie.
PavanPL KalyanK
Ranch Hand

Joined: Feb 28, 2009
Posts: 212
Bear thanks ,
 
Don't get me started about those stupid light bulbs.
 
subject: Need to perform an insert operation with AJAX
 
Similar Threads
A Query when using EJB Container Manestaged Transaction
Is it possible to start 20 Threads Simultaneosly under a JUnit TestCase
java-ajax for flax application
Spring + Hibernate
Good approach to perfrom a insert operation with