Connecting Tech Pros Worldwide Help | Site Map

XMLHttp request : responseXML is null while Post-ing

Sanjay Dahiya
Guest
 
Posts: n/a
#1: Jul 23 '05
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);
}
}
}
----

Martin Honnen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: XMLHttp request : responseXML is null while Post-ing




Sanjay Dahiya wrote:

[color=blue]
> this.request.onreadystatechange = this.handleStateChange;
>
> if( this.request) {
> this.request.open("POST", url ,true);
> this.request.setRequestHeader("Content-Type", "text/xml");[/color]

I would set onreadystatechange each time after the open call so move the
this.request.onreadystatechange = this.handleStateChange;
down here after the open call.
[color=blue]
> 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[/color]

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?

--

Martin Honnen
http://JavaScript.FAQTs.com/
Sanjay Dahiya
Guest
 
Posts: n/a
#3: Jul 23 '05

re: XMLHttp request : responseXML is null while Post-ing


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
---

-- markup from "request.responseText" --
<?xml version="1.0" encoding="UTF-8"?>
<root><child>Data</child></root>

Martin Honnen
Guest
 
Posts: n/a
#4: Jul 23 '05

re: XMLHttp request : responseXML is null while Post-ing




Sanjay Dahiya wrote:
[color=blue]
> 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
> ---[/color]

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.



--

Martin Honnen
http://JavaScript.FAQTs.com/
Sanjay Dahiya
Guest
 
Posts: n/a
#5: Jul 23 '05

re: XMLHttp request : responseXML is null while Post-ing


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

Closed Thread