Why can I not get XMLHttpRequest to work!!!

Discussion in 'XML' started by CES, Mar 29, 2005.

  1. CES

    CES Guest

    After three days I’m totally lost… I’m trying open an XML file in IE Mozilla and Safari. I was able to get one version to work in IE and Mozilla (MAC and Windows), the code that is commented out, but not Safari or Opera I then found that the preferred way to open is by using XMLHttpRequest but I can’t get any instance to work.

    I found the below code on Apples Dev Site and have seen variations on others sites however the line:

    xDoc.onreadystatechange = processReqChange;

    which I’ve seen in every XMLHttpRequest instance seems to cause the script to stop running but I'm sure most of the (window.XMLHttpRequest) is wrong.

    As you can tell I don’t have a clue and would greatly appreciate any help or at least a pointer to a VERY SIMPLE version of code that uses XMLHttpRequest, that actualy works..., Thanks in advance. - CES


    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>
    <script type="text/javascript">
    function loadXMLData(){
    var xmlFileName = 'demo.xml';
    var xDoc;
    if(window.ActiveXObject && /Win/.test(navigator.userAgent)){
    xDoc = new ActiveXObject("Microsoft.XMLDom");
    xDoc.async = false;
    xDoc.load(xmlFileName);
    writeXMLData(xDoc);
    return true;
    }
    /*
    // This works in IE and Mozilla (Windows and MAC) but not Safari
    else if(document.implementation && document.implementation.createDocument){
    xDoc = document.implementation.createDocument("","", null);
    xDoc.load(xmlFileName);
    xDoc.onload = function(){
    writeXMLData(xDoc);
    }
    return true;
    }
    */
    else if (window.XMLHttpRequest) {

    xDoc = new XMLHttpRequest();
    xDoc.onreadystatechange = processReqChange; // If this line is here it causes the code to stop working
    xDoc.open("GET", xmlFileName, true);
    xDoc.send(null);
    if (xDoc.readyState == 4) {
    if (xDoc.status == 200) {
    writeXMLData(xDoc);
    return true;
    }
    }
    alert("Mozilla Fork");
    }
    else{
    return false;
    }
    }
    </script>
    </head>
    <body onLoad="">
    <div id="xmlInfo">
    <script type="text/javascript">
    var e = document.getElementById("xmlInfo");
    function writeXMLData(xDoc){
    var movies = xDoc.getElementsByTagName("movie");
    for(var i = 0;i < movies.length;++i){
    e.appendChild(document.createElement("div")).innerHTML = movies.firstChild.nodeValue;
    }
    }
    if(! loadXMLData()){
    alert("NO XML");
    }
    </script>
    </div>

    </body>
    </html>
    CES, Mar 29, 2005
    #1
    1. Advertising

  2. CES

    Fred Oz Guest

    CES wrote:
    > After three days I’m totally lost… I’m trying open an XML file in IE
    > Mozilla and Safari. I was able to get one version to work in IE and
    > Mozilla (MAC and Windows), the code that is commented out, but not
    > Safari or Opera I then found that the preferred way to open is by using
    > XMLHttpRequest but I can’t get any instance to work.
    >
    > I found the below code on Apples Dev Site and have seen variations on
    > others sites however the line:
    >
    > xDoc.onreadystatechange = processReqChange;
    >
    > which I’ve seen in every XMLHttpRequest instance seems to cause the
    > script to stop running but I'm sure most of the (window.XMLHttpRequest)
    > is wrong.
    >
    > As you can tell I don’t have a clue and would greatly appreciate any
    > help or at least a pointer to a VERY SIMPLE version of code that uses
    > XMLHttpRequest, that actualy works..., Thanks in advance. - CES

    [...]

    Try this (no guarantees, however some good comments have been
    reported...):

    <URL:http://www.webpasties.com/xmlHttpRequest/>

    --
    Fred
    Fred Oz, Mar 29, 2005
    #2
    1. Advertising

  3. CES wrote:

    > After three days I’m totally lost… I’m trying open an XML file in IE
    > Mozilla and Safari. I was able to get one version to work in IE and
    > Mozilla (MAC and Windows), the code that is commented out, but not
    > Safari or Opera I then found that the preferred way to open is by using
    > XMLHttpRequest but I can’t get any instance to work.


    If all you want to do is load an XML document from a URL to have a
    scriptable XML DOM document then for best cross browser support use
    XMLHttpRequest in Mozilla, Safari 1.2, latest Konqueror I think, Opera
    8.00 Beta, and Microsoft.XMLHTTP in IE 5 and later:
    <http://www.faqts.com/knowledge_base/view.phtml/aid/6826/fid/616>

    If you want to exchange XML data between client and server:
    <http://www.faqts.com/knowledge_base/view.phtml/aid/17226/fid/616>


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 29, 2005
    #3
  4. CES

    CES Guest

    Martin,
    Thank you...

    I settled on the below script before I say your post but the two URL you provided will help me in the future!!! Thanks in your effort- CES


    <?xml version="1.0" encoding="iso-8859-1"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "xhtml11.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head>

    <script type="text/javascript">
    function loadXMLData(){
    var xmlhttp=false;
    var xmlFileName = "demo.xml";

    try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e){
    try {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e){
    xmlhttp = false;
    }
    }
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.open("GET", xmlFileName,true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4){
    if (xmlhttp.status == 200){
    writeXMLData(xmlhttp);
    return true;
    }
    else{
    alert("There was a problem retrieving the XML data:\n" + xmlhttp.statusText);
    }
    }
    }
    xmlhttp.send(null);
    }

    function writeXMLData(xmlData){
    //xmlhttp.responseXML returns XML
    var y = xmlData.responseXML;
    var x = y.getElementsByTagName('movie').item(0).firstChild.data;

    /*
    //xmlhttp.responseText returns a string that can be split into an Array
    // This Dosent work the way I need
    var y = xmlData.responseText.split(",");
    var x = y[0];
    */

    document.getElementById("xmlInfo").appendChild(document.createElement("div")).innerHTML = x;
    }
    </script>
    </head>
    <body onLoad="loadXMLData()">
    <div id="xmlInfo">
    </div>

    </body>
    </html>
    CES, Mar 30, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Horace Nunley

    why why why does function not work

    Horace Nunley, Sep 27, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    450
    =?Utf-8?B?UGV0ZXIgQnJvbWJlcmcgW0MjIE1WUF0=?=
    Sep 27, 2006
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    862
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  4. Xeno Campanoli
    Replies:
    1
    Views:
    331
    James Britt
    Jul 1, 2005
  5. CES
    Replies:
    3
    Views:
    110
Loading...

Share This Page