1

I have html page pulling data from another page using ajax.

The code works fine on firefox but gives an access denied on xhr.open("...")in IE and Chrome.

The sample code is as shown.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/Javascript">
        function changeContent(url)
        {
            var xhr = new XMLHttpRequest(); 
            xhr.open("GET",url,false); //Access denied on this line
            xhr.send();
            var roster = document.getElementById("roster");
            roster.innerHTML=xhr.responseText;
        }
    </script>
  </head>
  <body>
    <img src=images/logo_990x80.png width=1300" height="80" />

    <div class="buttonBar">
        <input type="button" value="data" onclick="changeContent('data.html')"/>
    </div>

    <div id="roster" class="roster">
        Click on the buttons above to choose a roster
    </div>

  </body>
</html>

The data.html contains a simple table with 2 rows of data.

How can I solve this issue.

Edit : Code shown below works on IE and firefox but still has the same issue in Chrome.It seems ActiveX works on local files for Ajax.

<!DOCTYPE html>
<html>
  <head>
    <script type="text/Javascript">
        function changeContent(url)
        {
        var xhr = false;
                    if(location.protocol=="file:")
        {
                if(!xhr)try{ xhr=new ActiveXObject("MSXML2.XMLHTTP"); }catch(e){xhr=false;}
                if(!xhr)try{ xhr=new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){xhr=false;}
            }
        else
        {
            if(!xhr)try{ xhr=new XMLHttpRequest(); }catch(e){xhr=false;}
        }   
            xhr.open("GET",url,false); //Access denied on this line only in Chrome
            xhr.send();
            var roster = document.getElementById("roster");
            roster.innerHTML=xhr.responseText;
        }
    </script>
  </head>
  <body>
    <img src=images/logo_990x80.png width=1300" height="80" />

    <div class="buttonBar">
        <input type="button" value="data" onclick="changeContent('data.html')"/>
    </div>

    <div id="roster" class="roster">
        Click on the buttons above to choose a roster
    </div>

  </body>
</html>

Any tips for chrome.

Dark Matter
  • 2,231
  • 3
  • 17
  • 31

1 Answers1

0

This is usually caused by trying to use XMLHTTPRequest without using an HTTP URI.

Firefox supports XHR over file: scheme URIs, most browsers do not.

Run your page through a web server if you want to use Ajax.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335