XMLHttpRequest Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cah2240

    XMLHttpRequest Question

    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.



    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.
  • Jim Ley

    #2
    Re: XMLHttpRequest Question

    On Fri, 24 Dec 2004 04:51:21 GMT, cah2240 <cah2240@mail.u sf.edu>
    wrote:
    [color=blue]
    >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.[/color]

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

    xmlhttp.send(xm lObj.xml)

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

    xml=Server.Crea teObject("Msxml 2.DOMDocument")
    xml.load(Reques t)

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

    Jim.

    Comment

    • Martin Honnen

      #3
      Re: XMLHttpRequest Question



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

      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.res ponseXML

      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

      Comment

      • Börni

        #4
        XMLHttpRequest recieving xml from php

        Martin Honnen:[color=blue]
        >
        > 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.res ponseXML
        >
        > 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.
        >[/color]

        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 createXMLFromSt ring () {
        var xmlParser, xmlDocument;
        try {
        xmlParser = new DOMParser();
        xmlDocument = xmlParser.parse FromString(xmlh ttp.responseTex t,
        '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

        Comment

        • Martin Honnen

          #5
          Re: XMLHttpRequest recieving xml from php



          Börni wrote:

          [color=blue]
          > - if the xml is created with server side scripting i need to do
          > something like this:
          >
          > function createXMLFromSt ring () {
          > var xmlParser, xmlDocument;
          > try {
          > xmlParser = new DOMParser();
          > xmlDocument = xmlParser.parse FromString(xmlh ttp.responseTex t,
          > 'text/xml');[/color]

          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">U mlaute: ä, ö, ü</text></root>';
          header('Content-Type: application/xml');
          echo utf8_encode($xm lString);
          ?>

          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

          Comment

          • Börni

            #6
            Re: XMLHttpRequest recieving xml from php

            Martin Honnen:[color=blue]
            > 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">U mlaute: ä, ö, ü</text></root>';
            > header('Content-Type: application/xml');
            > echo utf8_encode($xm lString);
            > ?>
            >[/color]

            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="UT F-8";

            Comment

            • Börni

              #7
              Re: XMLHttpRequest recieving xml from php

              [color=blue]
              > 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="UT F-8";[/color]

              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.

              Comment

              • Martin Honnen

                #8
                Re: XMLHttpRequest recieving xml from php



                Börni wrote:

                [color=blue]
                > 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="UT F-8";[/color]

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

                <?php
                $xmlDocument = new DOMDocument();

                $root = $xmlDocument->createElement( 'root');
                $text = $xmlDocument->createElement( 'text');
                $text->appendChild($x mlDocument->createTextNode (utf8_encode('U mlaute: ä,
                ö, ü')));
                $root->appendChild($t ext);
                $xmlDocument->appendChild($r oot);
                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

                Comment

                • Börni

                  #9
                  Re: XMLHttpRequest recieving xml [solved]

                  Martin Honnen wrote:[color=blue]
                  >
                  > We are getting off topic here in comp.lang.javas cript, but if I use the
                  > following example with PHP 5
                  >
                  > <?php
                  > $xmlDocument = new DOMDocument();
                  >
                  > $root = $xmlDocument->createElement( 'root');
                  > $text = $xmlDocument->createElement( 'text');
                  > $text->appendChild($x mlDocument->createTextNode (utf8_encode('U mlaute: ä,
                  > ö, ü')));
                  > $root->appendChild($t ext);
                  > $xmlDocument->appendChild($r oot);
                  > 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.
                  >[/color]

                  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.

                  Comment

                  Working...