Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AJAX ResponseXML coming back from a Servlet is null, but responseText has the XML

 
sam jacksonville
Greenhorn
Posts: 10
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I am new to the ranch. I have been trying my best to resolve this issue but I am not able to figure out the issue. Here is what I am trying to do.

1.I have a landing page for my app called index.jsp.
2.I have a form with a text box named "cardname" with a submit button
3.When I click the submit button, an AJAX POST call is made to a Servlet which returns an XML that has the payment details for the selected card.
4.The responseText coming back to the AJAX call shows the XML, however when I try responseXML I am getting a null. Here is what I have checked to make sure before opening up this question:
a. I have made sure there XML structure is fine (I think I am doing it right )
b. I used the alert on the javascript to show the xml that came back on responseText, it was fine
c. I used alert to show the XML that came back on the responseXML, but I got [object XML] message.

5. I could not attach the code, but the responseXML is below. Thanks for reading my post.

Here is the XML that the servlet returns. The XML is generated using JAXB inside the servlet.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CardPayments>
<card>Amex</card>
<payments>
<payment>
<id>3</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>44.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>4</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>12.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>5</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>12.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>6</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>12.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>7</id>
<date>2012-09-05T00:00:00-04:00</date>
<amount>1.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>8</id>
<date>2012-09-05T00:00:00-04:00</date>
<amount>1.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>9</id>
<date>2012-09-05T00:00:00-04:00</date>
<amount>1.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>10</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>89.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>11</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>89.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>12</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>12.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>28</id>
<date>2012-09-13T00:00:00-04:00</date>
<amount>84375.34</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>29</id>
<date>2012-09-13T00:00:00-04:00</date>
<amount>34.34</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>33</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>23.42</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>34</id>
<date>2012-09-04T00:00:00-04:00</date>
<amount>23.42</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>35</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>34.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>36</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>34.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>37</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>34.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>38</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>34.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>39</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>2.0</amount>
<cardType>Amex</cardType>
</payment>
<payment>
<id>40</id>
<date>2012-09-06T00:00:00-04:00</date>
<amount>2.0</amount>
<cardType>Amex</cardType>
</payment>
</payments>
</CardPayments>
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But probably neglected to set the content-type correctly?
 
sam jacksonville
Greenhorn
Posts: 10
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Bear. I have set the content type to response.setContentType("application/xml") in the servlet didn't work.

Even response.setContentType("text/xml") didn't help.


I am doing this in the javascript.

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you used the browser's debugger to inspect the response to make sure it's formed exactly as you intend?
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Open the url into the browser that the Ajax request is going to, that will normally show you the error in your XML.

Eric
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
XML generated by JAXB would assume to be correctly formed, but yeah, validating the XML is the next logical step if all of the response headers are correct.
 
sam jacksonville
Greenhorn
Posts: 10
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear,
Thanks you. I checked inside the browser now and I am seeing the XML in the xmlDoc, wooo hooo!. I have attached a screenshot of it. So looks like the XML is coming back in the response.

But now I am loading this in to the browser's DOM parser using the DOM Parser code:



I am getting the below parser error when I check in firebug.

 
g tsuji
Ranch Hand
Posts: 656
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You do not do ever something like this
xmlDoc=parser.parseFromString(xmlDoc,"text/xml");

or this
xmlDoc.loadXML(xmlDoc);

even we are pardonning the self-fabricated confusion of xmlDoc, one referring to .responseXML the other from being a reference to a parser instance created anew. The xmlDoc referring to .responseXML is an object not a String loosely speaking. So the last post is a regression rather than progression toward resolving a problem, whatever it is.

If you want to re-build the document and that you have .responseText error free, you can do that with .responseText (not with .responseXML).

If you stick the approach using responseXML, your successive posts are ambiguous. At some places, it seems you get responseXML non null, at others, null. In any case, if responseXML is corrected populated with the proper setting up server-side on the contentType, the only thing you should have to change is this.

The reason is that xmlDoc being the document object, its "firstChild" might not be the root element. So the result of that line is far from stable, and it might result in something else rather than the root element.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In other words, the XML has already been parsed and you are trying to re-parse a DOM.
 
sam jacksonville
Greenhorn
Posts: 10
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys...It all worked out fine...I learnt a lot from this. Appreciate your help Bear, g tsuji and Eric.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64631
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic