wood burning stoves 2.0*
The moose likes HTML, CSS and JavaScript and the fly likes Returning a Value from a Post 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 "Returning a Value from a Post" Watch "Returning a Value from a Post" New topic
Author

Returning a Value from a Post

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
I am using jQuery.post with a callback function. The callback function has on parameter. The servlet is assigning the parameter a value(pair). However, the value is an 'undefined' type when the callback function executes.

Calling code snipet:


Servlet Code snipet:
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9947
    
160

Moving this to our HTML/Javascript forum.


[My Blog] [JavaRanch Journal]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

The parameter passed to the $.post callback function will be the text of the response. You seem to think that that it will be some sort of object.

What is it that your servlet is writing to the response? Have you examined the response?

Running this sort of thing in Firefox with the Firebug plugin is an essential step in debugging any Ajax interaction. Firebug will show you exactly what the Ajax request is returning as the response.

P.S. You should reconsider naming your request parameter and your response parameter. Do you think that arg0 and arg1 make for the most readable code?
[ November 27, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
I changed the jQuery.post to jQuery.getJSON and it works now.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Do you understand the difference between the two methods? $.getJSON will automatically evaluate the response text as a JSON string. $.post returns the response text directly. Also, one performs a GET and one performs a POST. You should be mindful of which you want to use.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
Well, I thought this fixed my problem. Wrong. Using the get method, after I execute the servlet and get a result, the servlet cannot be exectued again. It is like the servlet will only run one time when using the doGet method. Example:

I log on as a particular user.
I get the data for a user and their activity for the month of November. OK

I log on as a different user and the try to get data for same month, the servlet will not produce a response. However, if I change to a different month it will work.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Your last question seems more like a servlet issue, so I suggest you post a new topic in the Servlets forum describing the problem. But first, contemplate the phrase "Using the get method" and the fact that your servlet only seems to have a doPost method (at least that you've shown).
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
In my doGet I have:

this.doPost(arg0, arg1);
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
After more testing using the post method I have discovered that the problem is in the value I am tring to return:

This will not work even though it is what I need.

String jReturnValues = "{eFlag:'NoError'}";

arg1.getWriter().write(jReturnValues);

This does work but I need several values sent.

String jReturnValues = "NoError";

arg1.getWriter().write(jReturnValues);
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

This will not work


"not work" needs to be defined.

That's perfectly valid JSON and will correctly be converted to a JS Object with a single property by $.getJSON.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
Okay once again I have ran different test and have a couple of things that still aren't right.

If I use the jQuery.post command with a callback. At the server I send back a string such as "{v1: 'test'}".

My callback function "callback(whatValue)" gets the string but the JS breaks when I try and use whatValue.v1 syntax.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60782
    
  65

Originally posted by Steve Dyke:

My callback function "callback(whatValue)" gets the string but the JS breaks when I try and use whatValue.v1 syntax.
Yes, as I said, the response is just text. You can't expect JS to just know when your string is a JSON string. It needs to be evaluated. Use of $.getJSON does this evaluation automatically. With any other Ajax method, you need to evaluate it yourself.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
Thanks again for your help.
 
Consider Paul's rocket mass heater.
 
subject: Returning a Value from a Post
 
Similar Threads
Passing an Array to iReport
Send parameter using XMLHttpRequest
Jasper report - ireport but in java class - dynamic report using jasper report api
Jasper report - ireport but in java class - dynamic report using jasper report api
Display a Chart from a Servlet to JSP