SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast rramroop's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XMLHttpRequest : Permission Denied

    Hello,

    I am experimenting with AJAX and I wanted to diplay the source code of a homepage or any other html file into a text area.

    When I run the script, I get no response in FireFox and a script error in IE saying: Permission is denied.

    here is the code:

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html>
    <head>
    <title>AJAX XMLHttpRequest</title>
    <meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1' />
    <script language="javascript" type="text/javascript">
    
    var request = false;
    
    try {
        request = new XMLHttpRequest();
    }
    
        catch (trymicrosoft)
    {
        try
        {
            request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (othermicrosoft)
        {
            try {
                request = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (failed)
            {
                request = false;
            }
        }
    }
    
    if (!request)
      alert("Error initializing XMLHttpRequest!");
    
    function getWebAddress() {
        var url = "http://www.google.co.uk/";
        request.open("GET", url, true);
        request.onreadystatechange = updatePage;
        request.send(url);
    }
    
    function updatePage() {
        if (request.readyState == 4)
            if (request.status == 200) {
                var response = request.responseText;
                document.getElementById("txt").value = response;
            }
            else if (request.status == 404)
                alert("Requested URL does not exist");
            else
                alert("Error: Status code is " + request.status);
        }
    
    </script>
    </head>
    <body>
    <h1>AJAX XMLHttpRequest</h1>
    <form action="POST">
    <textarea id='txt' cols=50 rows=20></textarea> <br />
    <input type="button" name="address" id="address" value="getOutput" onclick="getWebAddress();" /><br />
    </form>
    </body>
    </html>
    Can anyone please help?

    many thanks

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't use XMLHttpRequest to access resources on another domain than the one where the script is executing. (For security reasons.)
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast rramroop's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I've read as welll..

    but I've got an assignment to do and it's as follows:

    Retrieve a response to “GET” request from a webserver (http://www.google.co.uk for example) and place the response text into the <textarea>. This should work in IE, Firefox and Opera but note, if the test page 2 HTML file is rendered not from a server (from local harddrive for example) the code will most likely fail due to security violation – this is to be expected. Code should be in javascript and should utilise XMLHttpRequest [and Microsoft’s ActiveX version - ActiveXObject("MSXML2.XMLHTTP")]. The request should be handled asynchronously. The result should look similar to Picture 2.
    Picture 2:



    How do I achieve that?

    many thanks

    regards

  4. #4
    SitePoint Enthusiast
    Join Date
    Nov 2005
    Location
    London, UK
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe what they mean is that you fetch the remote page html from your local server instead of your local client. Once fetched, your local server can give it to your local client. Javascript like this maybe:

    Code:
    function getWebAddress() {
        var url = "/sourcefetcher.cgi";
        request.open("GET", url, true);
        request.onreadystatechange = updatePage;
        request.send("?url=http://www.google.co.uk");
    }

  5. #5
    SitePoint Addict
    Join Date
    Jul 2006
    Location
    Kansas City, MO
    Posts
    280
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if the test page 2 HTML file is rendered not from a server (from local harddrive for example) the code will most likely fail due to security violation – this is to be expected

    Try reading your assignment. It specifically says you can't access non-local (domain) content.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only workaround I know of is to use a server-side script, as peterbe suggests. Your Ajax code will call the script (on your domain) and that can then retrieve the information from an external source and return it to your JavaScript function.
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Enthusiast rramroop's Avatar
    Join Date
    May 2003
    Location
    UK
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks,

    I am writing a php script to fetch the page with the fopen() function.
    I hope it works.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •