Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Post Does not Return new Values

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I run thia javascript to first time on the form it works great. However if I try a different jobnumber without refreshing the page the values returned are the same as first try. Any ideas?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you really not code with indentation? I'm having a surprisingly difficult time reading your code and figuring out what statement belongs in which function.
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anywhere within your reponse handler that you obtain the results of the Ajax request.

Oh, and your Ajax call should be:


The second parameter should be a hash object rather than a string that you format yourself.
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes my code does have indentation. I left justified all the lines when I posted the code. Sorry, it won't happen again. Do you think from what I have posted you could help me out with a solution?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See my 2nd reply.

P.S. You can fix the code in your post by clicking .
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the servlet that the jQuery is calling I set session attributes to the values that an SQL connection returns. The calling AJAX process sets the values of the form elements to the session values using expression code. I take it that this is not the correct way to do this? I need to return two values.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Client-side code has no access to the session. What code in your handler are you thinking is accessing the session?

If it's the ${} construct, think again. That construct is evaluated on the server when the page is first sent to the browser. Do a View Source on the page and you will see that by the time the page hits the browser, all ${} constructs have been replaced with their evaluations.

You might want to review this article regarding how JSPs work.

Any values that you want to return from your Ajax request need to be returned in the response. It's easy to return multiple values using XML or JSON. I use the latter almost exclusively because it's a lot easier to deal with in JavaScript code. And, jQuery supports it directly.

P.S. What you are doing is clearly a "get" operation, why are you using a post?
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give me an example on how to set this up?

What do I need on the client(javascript) side?
What do I do with my servlet code so it returns values back to calling page?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since you are using jQuery it's easy. Check out the Ajax API's for jQuery and you'll find one that's well-suited for returning JSON.

In your servlet, format the JSON construct to contain the data value that you need and return it as the value of the request.
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After much reading I can see(I think) how to send a getJSON() request to server. Still not sure if it would be jQuery.getJSON or $.getJSON. But if the key/value pairs are to give the server parameters with which to work. How do I tie the response back to the calling AJAX method?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Steve Dyke:
Still not sure if it would be jQuery.getJSON or $.getJSON.

$ is merely an alias for jQuery so they are completely equivalent.

Sounds like you could use a good jQuery reference. There are a couple of books already published, and my own book, jQuery in Action, will be published in December. It is available as an ebook now via the Manning Early Access Program, if you are interested.

How do I tie the response back to the calling AJAX method?

Same way as with the $.post() method, via the completion callback. See the API for $.getJSON() for details.
[ November 07, 2007: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can get my JSON call to work so the servlet will gather the data I need. How do I set up the Servlet so the JavaScript function can read its values? I found this example but nothig on how to set up the called servlet.

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In an earlier post You said to use the JSON construct. Where can I find the JSON library to add to my workbench. I can find parts of it on the iternet but not the whole package.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually just build it "by hand". But I've used the StringTree JSON generator now and again to good effect.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JSON is pretty simple. You mentioned that you need to return two values. Let's call the vales a and b. The JSON is as simple as:

 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, I have the json.jar added to my workbench. I have inported it to my servlet. Now how do I set the response write string to a JSONString or do I use the JSONObject?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You'll have to read the docs for the JSON library that you're using.

(Again, for something simple like two values, I'd just build the string myself).
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got another problem now. I added the json.jar to workbench, set up the code in servlet. Now when I run the app I get an error. How do I find the JSON classes if they are in another package if this is what it is telling me?

[11/8/07 14:42:02:327 CST] 00000040 WebApp E SRVE0026E: [Servlet Error]-[GetCustomerListServlet]: java.lang.Exception: java.lang.LinkageError: LinkageError while defining class: org.json.JSONException
Could not be defined due to: org/json/JSONException (Unsupported major.minor version 50.0)
This is often caused by having a class defined at multiple
locations within the classloader hierarchy. Other potential causes
include compiling against an older or newer version of the class
that has an incompatible method signature.
 
Steve Dyke
Ranch Hand
Posts: 1679
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes I can't see the forest for the trees. I finally realized what you were trying to tell me concerning "hand writing" the return string. I killed the json.jar from my workbench. Defined the SIMPLE return string and it works great.

Thanks for ALL the help even when my brain was in sleep mode.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65225
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic