0

The following javascript function works fine for IE, Safari and Firefox. But it fails in Chrome(33.0.) and Opera (16.0.1196). Blank HTML page is displayed on loading.

function readTestXMLFile() {

    if (window.ActiveXObject) { 
        var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
        xmlDoc.async = 'false';
        xmlDoc.load('test.xml');
    }
    else {     
        var requ = new XMLHttpRequest();
        alert("a");

        requ.open("GET", "test.xml", false);
        alert("b");

        requ.send(null);    //This line is not working in chrome and opera
        alert("c");

        var xmlDoc = requ.responseXML;
        alert(xmlDoc);
        alert("d");
    }

    return xmlDoc;
}

Only 'a' and 'b' gets printed. It does not continue after that. Same result is observed if I use requ.send() or requ.send("") instead of requ.send(null).

If I remove the statement requ.send(null), then 'null' value is printed for xmlDoc. Still blank HTML loads.

Please let me know what is the right way to get this work on Chrome and Opera.

Thanks

SRB.

Mathias
  • 5,642
  • 2
  • 31
  • 46
srb
  • 5
  • 1
  • 1
  • 4
  • If you look in the browser console for Chrome, what message do you get? The only error I get is that test.xml doesn't exist, which is expected. – Mathias Mar 03 '14 at 07:25
  • I get "Cross origin requests are only supported for HTTP." error message in the console. Please let me know what I should do to get out of this? Note: I have only this function call in java script. No other function calls. With the return value from this function I am not doing anything currently (for debugging purpose). The HTML loads successfully if I remove the call to this function. Thanks – srb Mar 03 '14 at 07:43

3 Answers3

1

Your error message suggest that you are trying to access a local file which is treated as "Cross origin request" if you try and run local server it should work.

Take a look at this previously asked question with the same problem: Cross origin requests are only supported for HTTP but it's not cross-domain

Then you would access http://localhost/.../test.xml instead of c:/localhost/.../test.xml

You can also set a flag for Chrome to allow local files to request local files: -allow-file-access-from-files

Community
  • 1
  • 1
Mathias
  • 5,642
  • 2
  • 31
  • 46
  • Yes. I am accessing the 'test.xml' local file which is kept along with other html files. When I open the html file in the browser it is suppose to read the test.xml file and display some contents. I am not using any server to do so. Both html and xml file get installed in the end user system. So the user installs these files and opens html file which inturn reads the xml file and displays some contents. The end user need not have any network connection for this purpose. Everything should work locally. So is there any way to solve this problem? Please let me know.- Thanks – srb Mar 03 '14 at 08:09
  • I am just thinking why it fails only in chrome and opera. And works on other browsers? - Thanks – srb Mar 03 '14 at 08:09
  • 1
    It depends on how the different browsers treats request to local files. If you look at the question I linked to they recommend a few other solutions like setting flags in Chrome, -allow-file-access-from-files – Mathias Mar 03 '14 at 08:18
  • Thank you for the answer. I helped me solve the problems with Chrome and Opera. - Thanks, SRB – srb Mar 03 '14 at 09:21
  • 1
    You should vote on the answers that helped you to let other users know which ones were helpful. – Mathias Mar 03 '14 at 10:08
  • Yes I tried to vote up. But it gives me message that minimum of 15 reputations are required to vote up. As I have only 5 reputations I could not vote the answers. – srb Mar 03 '14 at 11:27
0

the call to the XMLHttpRequest.send method is Asynchronous so you need to modify the call a little bit. The modified code below will print the response content when the response is returned successfully:

 requ.addEventListener("load", function(e) {
           alert(req.responseText);
        }, false)

 requ.send(null);

Update: I didn't notice that you made the send request call synchronous.

Edit You need to launch chrome with this parameter to be able to access local files

--allow-file-access-from-files

ex: c:\Browser\chrome.exe --allow-file-access-from-files

Rami
  • 7,162
  • 1
  • 22
  • 19
  • 1
    The call that OP is making is actually synchronous as he has `false` as the third parameter in `open` – Mathias Mar 03 '14 at 07:24
  • Thanks for the reply. I had tried this solution before but did not change anything. Nothing is printed. The responseText does not get printed in Chrome and Opera. But the responseText get printed in Firefox and Safari successfully. – srb Mar 03 '14 at 07:31
  • I get "Cross origin requests are only supported for HTTP." error message in the console. Please let me know what I should do to get out of this? Note: I have only this function call in java script. No other function calls. With the return value from this function I am not doing anything currently (for debugging purpose). The HTML loads successfully if I remove the call to this function. - Thanks – srb Mar 03 '14 at 07:48
  • Where is the xml file located? – Rami Mar 03 '14 at 07:56
  • I am accessing the 'test.xml' local file which is kept along with other html files. When I open the html file in the browser it is suppose to read the test.xml file and display some contents. I am not using any server to do so. Both html and xml file get installed in the end user system. So the user installs these files and opens html file which inturn reads the xml file and displays some contents. The end user need not have any network connection for this purpose. Everything should work locally. - Thanks – srb Mar 03 '14 at 08:11
  • I am just thinking why it fails only in chrome and opera. And works on other browsers? If it is a problem with xml file or accessing local file or some path related issues then the same problem should have appeared even for other browsers, which is not the case here. So it must be something that is specific to chrome and opera. – srb Mar 03 '14 at 08:13
  • Thank you very much for the answer. It helped me solve the problem of both Chrome and Opera. But how I can expect the end user to launch the html file in Chrome/Opera using this command? They simply launch the html file in Chrome - – srb Mar 03 '14 at 08:45
  • @srb: In that case you will need to prepare a new launcher program that calls the chrome/opera executable file passing the extra argument – Rami Mar 03 '14 at 08:51
0

I think that the problem is that you're passing null to the send() method. You are making a GET request, so you should call send without parameters. I think chrome throws an exception because of that. Just remove the null

Oscar Paz
  • 18,084
  • 3
  • 27
  • 42
  • Same result is observed if I use requ.send() or requ.send("") instead of requ.send(null). This did not solve the problem. - Thanks – srb Mar 03 '14 at 07:45