By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,341 Members | 1,720 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,341 IT Pros & Developers. It's quick & easy.

XMLHttpRequest Question

P: n/a
I have been searching all over the place for a good full explanation of
what can be passed back and forth using the XMLHttpRequest Object.

http://jibbering.com/2002/4/httprequest.html

That site has a good intro on what it can do to receive one piece of
information back. However is it possible to pass more? This object is
supposed to be able to pass a full DOM tree, but I haven't found any specs
on what the tree should be sent as from the server or how to parse it
after I receive it.
Jul 23 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On Fri, 24 Dec 2004 04:51:21 GMT, cah2240 <ca*****@mail.usf.edu>
wrote:
That site has a good intro on what it can do to receive one piece of
information back. However is it possible to pass more? This object is
supposed to be able to pass a full DOM tree, but I haven't found any specs
on what the tree should be sent as from the server or how to parse it
after I receive it.


use POST or PUT and send a text serialisation of the DOM:

xmlhttp.send(xmlObj.xml)

and it's sent unencoded, you can load it into an XML document on the
server with something like:

xml=Server.CreateObject("Msxml2.DOMDocument")
xml.load(Request)

In php it will be in $HTTP_RAW_POST_DATA, and you can parse it into an
XML object.

Jim.
Jul 23 '05 #2

P: n/a


cah2240 wrote:
I have been searching all over the place for a good full explanation of
what can be passed back and forth using the XMLHttpRequest Object.

http://jibbering.com/2002/4/httprequest.html

That site has a good intro on what it can do to receive one piece of
information back. However is it possible to pass more? This object is
supposed to be able to pass a full DOM tree, but I haven't found any specs
on what the tree should be sent as from the server or how to parse it
after I receive it.


If the server sends XML (which is usually judged by the Content-Type
header it sends, e.g. application/xml or text/xml and for Mozilla
probably application/xhtml+xml) then the XMLHttpRequest object parses
the HTTP response body automatically and makes the DOM document available as
httpRequest.responseXML

You might want to check
<http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
for more details.

As for the server side scripting you need to send a HTTP response with
the serialized XML in the response body and the response headers
declaring the Content-Type e.g. application/xml.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #3

P: n/a
Martin Honnen:

If the server sends XML (which is usually judged by the Content-Type
header it sends, e.g. application/xml or text/xml and for Mozilla
probably application/xhtml+xml) then the XMLHttpRequest object parses
the HTTP response body automatically and makes the DOM document
available as
httpRequest.responseXML

You might want to check
<http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>
for more details.

As for the server side scripting you need to send a HTTP response with
the serialized XML in the response body and the response headers
declaring the Content-Type e.g. application/xml.


I'm also trying to parse some xml which is generated by php and then
send back again. But now i am a bit confused.
In php i set the header to application/xml and then just echo it. But
responseXML just has one child with the value null, thereas responseText
contains the complete xml i generated.
From the AppleDev Website i have an example which parses an xml
document and they access it with normal dom methods. The difference is
that they request an already existing xml file from the server.

Lets see if i understand it right:
- if i request a regular file containing xml from the server, then it is
accessible with dom methods in responseXML.
- if the xml is created with server side scripting i need to do
something like this:

function createXMLFromString () {
var xmlParser, xmlDocument;
try {
xmlParser = new DOMParser();
xmlDocument = xmlParser.parseFromString(xmlhttp.responseText,
'text/xml');
return xmlDocument;
}
catch (e) {
output("Can't create XML document.");
return null;
}
}

and then i can access xmlDocument with regular dom methods?
Would be great if you could clear things up for me
Jul 23 '05 #4

P: n/a


Börni wrote:

- if the xml is created with server side scripting i need to do
something like this:

function createXMLFromString () {
var xmlParser, xmlDocument;
try {
xmlParser = new DOMParser();
xmlDocument = xmlParser.parseFromString(xmlhttp.responseText,
'text/xml');


No, I see no need to parse responseText, if the XML sent to the browser
has the proper Content-Type header and the encoding is properly declared
then responseXML should work. The browser doesn't really know or care
whether that XML sent to it stems from a static XML document the HTTP
server sends or from a PHP page run on the server.
Thus if you have any problems that the response from your PHP script is
not properly parsed by the browser then I would guess that there is
something wrong with your PHP trying to send XML.
PHP is byte oriented and mainly geared to create output in 8bit
encodings like ISO-8859-1 while XML parsers are not required to
understand such encodings with XML favouring Unicode and encodings such
as UTF-8 or UTF-16.
Thus if you want to output XML with a PHP page in an 8bit encoding you
might want to do it as follows:

<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
$xmlString .=
'<root><text xml:lang="de">Umlaute: ä, ö, ü</text></root>';
header('Content-Type: application/xml');
echo utf8_encode($xmlString);
?>

That way I have no problems here with the generated XML, whether I use
browsers like Mozilla, IE 6, Opera 8.00 beta to directly render the XML
sent from the PHP or whether I try to consume the XML with the
XMLHttpRequest object and access responseXML.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #5

P: n/a
Martin Honnen:
PHP is byte oriented and mainly geared to create output in 8bit
encodings like ISO-8859-1 while XML parsers are not required to
understand such encodings with XML favouring Unicode and encodings such
as UTF-8 or UTF-16.
Thus if you want to output XML with a PHP page in an 8bit encoding you
might want to do it as follows:

<?php
$xmlString = '<?xml version="1.0" encoding="UTF-8"?>' . "\r\n";
$xmlString .=
'<root><text xml:lang="de">Umlaute: ä, ö, ü</text></root>';
header('Content-Type: application/xml');
echo utf8_encode($xmlString);
?>


Well, actually i am working in php5, there i have the new DomDocument
object. And so then i say "echo DomDocument->savexml()" it should also
be an utf-8 string. I even tried to explicitely set the encoding:
DomDocuemnt->encoding="UTF-8";
Jul 23 '05 #6

P: n/a
Well, actually i am working in php5, there i have the new DomDocument
object. And so then i say "echo DomDocument->savexml()" it should also
be an utf-8 string. I even tried to explicitely set the encoding:
DomDocuemnt->encoding="UTF-8";


Never Mind. The encoding is right and all.
The problem seems to be that the xml gets passed through another php
file before it finally reaches the browser.
But thats too offtopic.
Jul 23 '05 #7

P: n/a


Börni wrote:

Well, actually i am working in php5, there i have the new DomDocument
object. And so then i say "echo DomDocument->savexml()" it should also
be an utf-8 string. I even tried to explicitely set the encoding:
DomDocuemnt->encoding="UTF-8";


We are getting off topic here in comp.lang.javascript, but if I use the
following example with PHP 5

<?php
$xmlDocument = new DOMDocument();

$root = $xmlDocument->createElement('root');
$text = $xmlDocument->createElement('text');
$text->appendChild($xmlDocument->createTextNode(utf8_encode('Umlaute: ä,
ö, ü')));
$root->appendChild($text);
$xmlDocument->appendChild($root);
header('Content-Type: application/xml');
echo $xmlDocument->saveXML();
?>

then browsers and their XMLHttpRequest objects deal with the outputted
XML just fine.

If you still run into problems then try a PHP group.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #8

P: n/a
Martin Honnen wrote:

We are getting off topic here in comp.lang.javascript, but if I use the
following example with PHP 5

<?php
$xmlDocument = new DOMDocument();

$root = $xmlDocument->createElement('root');
$text = $xmlDocument->createElement('text');
$text->appendChild($xmlDocument->createTextNode(utf8_encode('Umlaute: ä,
ö, ü')));
$root->appendChild($text);
$xmlDocument->appendChild($root);
header('Content-Type: application/xml');
echo $xmlDocument->saveXML();
?>

then browsers and their XMLHttpRequest objects deal with the outputted
XML just fine.

If you still run into problems then try a PHP group.


Well, actually the reason why i wasted the last to hours is kind a
funny: one of the included php files contained a linebreak after the ?>.

And that just somehow broke everything.
Its still offtopic, but its too amusing.
Jul 23 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.