• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Returning a Value from a Post

 
Steve Dyke
Ranch Hand
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving this to our HTML/Javascript forum.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Steve Dyke
Ranch Hand
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I changed the jQuery.post to jQuery.getJSON and it works now.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my doGet I have:

this.doPost(arg0, arg1);
 
Steve Dyke
Ranch Hand
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65119
91
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1643
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks again for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic