XMLHttp request : responseXML is null while Post-ing

S

Sanjay Dahiya

I tried POSTing from XMLHttpRequest, i can get the XML right on server
but responseXML from server is coming null. I can see the XML right in
responseText. but responseXML is null. responseText to DOM conversion
also fails while the XML in responseText seems valid ..

-- here is the javascript code for sending ---
{
this.request.onreadystatechange = this.handleStateChange;

if( this.request) {
this.request.open("POST", url ,true);
this.request.setRequestHeader("Content-Type", "text/xml");

var markup = serialize(doc);
this.request.send(markup);
}
---
it works fine and I can get XML on server. but from server I write the
same XML back -
---
InputStream is = request.getInputStream();
Document doc = XMLUtils.load(is, false);

response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");

PrintWriter writer = response.getWriter();
BufferedWriter bufWriter = new BufferedWriter(writer);

XMLUtils.printDocument(doc, bufWriter);
bufWriter.newLine();

response.flushBuffer();
bufWriter.close();
---
I can receive the XML document right in responseText but responseXML is
null. cant make out whats happening here..

----
function processReqChange()
{
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
alert(req.responseText);
handler.handle(req.resposeXML); // req.responseXML is NULL
HERE
} else {
alert("There was a problem retrieving the XML data:\n" +
req.statusText);
}
}
}
----
 
M

Martin Honnen

Sanjay Dahiya wrote:

this.request.onreadystatechange = this.handleStateChange;

if( this.request) {
this.request.open("POST", url ,true);
this.request.setRequestHeader("Content-Type", "text/xml");

I would set onreadystatechange each time after the open call so move the
this.request.onreadystatechange = this.handleStateChange;
down here after the open call.
var markup = serialize(doc);
this.request.send(markup);
}
---
it works fine and I can get XML on server. but from server I write the
same XML back -
---
InputStream is = request.getInputStream();
Document doc = XMLUtils.load(is, false);

response.setCharacterEncoding("UTF-8");
response.setContentType("text/xml");

PrintWriter writer = response.getWriter();
BufferedWriter bufWriter = new BufferedWriter(writer);

XMLUtils.printDocument(doc, bufWriter);
bufWriter.newLine();

response.flushBuffer();
bufWriter.close();
---
I can receive the XML document right in responseText but responseXML is
null. cant make out whats happening here..

----
function processReqChange()
{
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
alert(req.responseText);
handler.handle(req.resposeXML); // req.responseXML is NULL
HERE

Could you check the response headers e.g.
alert(req.getAllResponseHeaders())
to the see the content type header that is sent?
And are you sure the markup sent is well-formed?
 
S

Sanjay Dahiya

thanks Martin,
I tried it, doesnt seem to work for me, yes the markup is well formed,
i tried a very small example which fails. here are the headers and
markup that i get on client (after POST). can "chunked" encoding cause
problem, i googled for it and didnt find anything useful ..

---header --
Content-Type: text/xml;charset=UTF-8
Transfer-encoding: chunked
 
M

Martin Honnen

Sanjay said:
yes the markup is well formed,
i tried a very small example which fails. here are the headers and
markup that i get on client (after POST). can "chunked" encoding cause
problem, i googled for it and didnt find anything useful ..

---header --
Content-Type: text/xml;charset=UTF-8
Transfer-encoding: chunked
---

The definition is here in the HTTP 1.1 specification
<http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.6>

Which browser are you using, is that a Mozilla browser?
You seem to use Java on the server, I don't know how to get your servlet
or JSP not to use Transfer-encoding: chunked but maybe you can switch
that off and try to see whether it improves things.
As far as I understand it any user agent supporting HTTP 1.1 should be
able to deal with Transfer-encoding: chunked but perhaps there is a bug
related to that.
 
S

Sanjay Dahiya

I tried it on both IE6 and Firefox 1.0.2, yes I am using tomcat. will
try to switch chunked off .. and leave a msg if that works.
thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top