Connecting Tech Pros Worldwide Help | Site Map

XMLHttp request : responseXML is null while Post-ing

  #1  
Old July 23rd, 2005, 08:07 PM
Sanjay Dahiya
Guest
 
Posts: n/a
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);
}
}
}
----

  #2  
Old July 23rd, 2005, 08:07 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #3  
Old July 23rd, 2005, 08:07 PM
Sanjay Dahiya
Guest
 
Posts: n/a

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>

  #4  
Old July 23rd, 2005, 08:07 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #5  
Old July 23rd, 2005, 08:07 PM
Sanjay Dahiya
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Javascript not working on my PC but ok on others drwyness answers 16 August 22nd, 2008 12:04 AM
Xpath In Javascript chandrashekhar maral answers 23 September 12th, 2007 11:43 AM
how to request to servlet from Ajax santhoskumara answers 6 July 7th, 2007 12:46 PM
Using XPATH in Mozilla newToAjax answers 1 June 29th, 2007 09:41 AM
AJAX not working when using Session variables. =?Utf-8?B?U2hhd24gU2VzbmE=?= answers 6 May 31st, 2007 11:25 PM