Reading XML web services with Javascript

Hi all,

I am trying to create a web page that reads a web service which presents a page in XML - (you go to a URL with the right parameters in the querystring and get a page of XML). I will eventually apply transformations on the XML to give a nice looking page, but for now I am having trouble just reading the data.
The error I get is:
In FireFox:
Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]

In IE:
Access is denied.

I know the XML page works - I can see it in the browser.
I know it is accessible to web site I am adding the page to as I built a Flex app to read it previously from the same location.
So the error must be my code.

And here is the code:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>XML</title>

    <script language="javascript" type="text/javascript">
function xmlCall()
{
   var xmlDoc;
   var xmlhttp;
   var url = "";
	
    if (window.XMLHttpRequest)
    {
        xmlhttp=new XMLHttpRequest();
    }
    else
    {
        try
        {
            xmlhttp=new ActiveXObject("MSXML2.ServerXMLHTTP");
        }
        catch (e) {}
        try
        {
            xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.6.0");
        }
        catch (e) {}
        try
        {
            xmlhttp=new ActiveXObject("MSXML2.XMLHTTP.3.0");
        }
        catch (e) {}
        try
        {
            xmlhttp=new ActiveXObject("MSXML2.XMLHTTP");
        }
        catch (e) {}
        try
        {
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e) {}
        throw new Error("This browser does not support XMLHttpRequest.");
    }
    xmlhttp.open("GET",url,false);
    try
    {
    xmlhttp.send();
    }
    catch (e) { alert(e.message); };
    xmlDoc = xmlhttp.responseXml;
}

    </script>

</head>
<body onload="xmlCall();">
</body>
</html>

I know url is blank - the web service is not yet public so I can’t publish the URL - but I would guess this should work with any XML page.
I have looked around for answers without luck, any help is appreciated.

Keep in mind cross-domain security restrictions in the browsers - is url the same as the domain that serves your page witht he javascript?

You may need to have a relay script on your own server to work around that.

It isn’t in the same domain, but I have a Flex app using the same XML on a totally different domain and it works fine. Is there something extra I need to set for JavaScript?

I’m not familiar with flex - is it java script based?

The security settings in your browser will determine if java script is prevented from accessing other domains - I think that by default IE and FF will block cross-domain access.

Try doing a copy and paste of the XML onto your own server and see if the script will work if the domai is teh same… should take 5 minutes to try…

Well Flex is Flash based and it works - I also checked with VB Script and that works.
I tried your suggestion and it did work, I can read XML files from my local machine with JavaScript but not from the web service, but I’m using the same browser settings as when I look at the VB and Flash versions. Is there any code I can change or which browser settings do I need to change to make this possible?
Thanks for helping so far

Hi,

I prefer to leave browser security setting alone - unless your site is for a very closed audience you can’t expect visitors to have to change settings just to see your site!

If you goodle for this topic you will find some people coming up with work arounds - e.g: http://ejohn.org/blog/cross-site-xmlhttprequest/

I googled for: “cross site javascript enable in firefox”

You already found one potential solution - a relay script on your own domain.

Good luck!