my dog learned polymorphism*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX script isn't parsing XML response from servlet 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 "AJAX script isn Watch "AJAX script isn New topic
Author

AJAX script isn't parsing XML response from servlet

Tony Beseau
Greenhorn

Joined: Aug 31, 2011
Posts: 3
Hello all,

I am somewhat new to Java servlet programming. I am using Eclipse Indigo Release IDE and it is running the Tomcat 7 server. The client is using Firefox 7.0.1. I can get the JSP file to load the Javascript function and it successfully sends a POST to the servlet. The servlet successfully queries a MySQL database using a JDBC connection and creates the XML responseString. the problem I am having is that I cannot determine if the string is successfully being returned to the JSP file or if the Javascript file is configured correctly to parse the XML string. The data I want to display on the web page does not get displayed. Any suggestions as to what I am doing wrong? Thank you for the help.

Servlet:


responseString = The responseString is actually one long string. I added the formatting below for readability




Javascript: getInventory.js



JSP file: inventory.jsp

Karthik Shiraly
Ranch Hand

Joined: Apr 04, 2009
Posts: 492
    
    5
One problem I can spot is in this line:


The intention is to set a callback function to be called asynchronously by browser.
But due to the parentheses, what this actually does is make a function call immediately to gotInventoryCallback()
(which is why you don't see the alert in that function), and then assign its return value (null value) to req.onreadystatechange, which is meaningless.

The correct code should be



This is the one I could spot, but I didn't go through your XML implementation in detail. Try out this correction and if there are further problems, ask away on this thread.
Also, IMO, you should stop using the raw XMLHttpRequest API and start with jquery (it's not the only one out there, but it's good to start with). It helps write much more compact, readable, cross-browser error free code.
In jquery, this would have been
Tony Beseau
Greenhorn

Joined: Aug 31, 2011
Posts: 3
Karthik,

Thank you for the reply. Removing the parentheses allowed the alerts to work in the gotInventoryCallback function. Parsing the XML data and populating the fields still doesn't work. I have barely seen JQuery so I am not sure how to get it setup. Do you have a link to a good example that could show me how to set that up? Also, to use JQuery do I load the jquery.min.js file in the JSP file or in the javascript file?

Thanks again,
Tony
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

It would be very helpful if you could get rid of the "JSP" from your question, since it's irrelevant. Your JSP runs on the server and generates HTML, which is sent to the browser. This HTML can of course include Javascript, as it does in your example. So we're really just discussing that HTML and the Javascript. Referring to the "JSP" just inserts more confusion into the issue.

Now, you said
the problem I am having is that I cannot determine if the string is successfully being returned to the JSP file or if the Javascript file is configured correctly to parse the XML string.


So it's time to find out whether your Javascript is receiving that XML data or not. You need a Javascript debugger. Firebug in Firefox works very well but I'm sure there are others out there.

And since this question seems to be almost entirely about Javascript, I'm going to move it to the Javascript forum.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60817
    
  65

Every modern browser except for Firefox has a built-in JavaScript debugger. And as Paul pointed out, there's the Firebug plugin for Firefox.

Trying to debug JavaScript without using the debuggers is like trying to play concert piano wearing welding gloves. Get to know your tools.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Tony Beseau
Greenhorn

Joined: Aug 31, 2011
Posts: 3
Maybe playing piano with welding gloves is good music.

As I mentioned I am new to Java web development. I learned something today though and that is how to use the Javascript debugger in Firefox. As Paul and Bear pointed out it was a very useful tool and helped me solve my issue. The data is now getting populated in the web browser as I need it to. Thank you for your help and comments.

Tony

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: AJAX script isn't parsing XML response from servlet
 
Similar Threads
parsing the xml returned from ajax call
Display / Hide Table based on output
Passing Value from Javascript method into HTML Body
unable to access the values of dynamically newly added rows to table
Couldn't find error in this code