aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX ResponseXML coming back from a Servlet is null, but responseText has the XML Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX ResponseXML coming back from a Servlet is null, but responseText has the XML" Watch "AJAX ResponseXML coming back from a Servlet is null, but responseText has the XML" New topic
Author

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

sam jacksonville
Greenhorn

Joined: Sep 02, 2012
Posts: 10

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

Joined: Jan 10, 2002
Posts: 61202
    
  66

But probably neglected to set the content-type correctly?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
sam jacksonville
Greenhorn

Joined: Sep 02, 2012
Posts: 10

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

Joined: Jan 10, 2002
Posts: 61202
    
  66

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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Jan 10, 2002
Posts: 61202
    
  66

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

Joined: Sep 02, 2012
Posts: 10

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

Joined: Jan 18, 2011
Posts: 512
    
    3
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

Joined: Jan 10, 2002
Posts: 61202
    
  66

In other words, the XML has already been parsed and you are trying to re-parse a DOM.
sam jacksonville
Greenhorn

Joined: Sep 02, 2012
Posts: 10

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

Joined: Jan 10, 2002
Posts: 61202
    
  66

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AJAX ResponseXML coming back from a Servlet is null, but responseText has the XML