Borort

The sky’s the limit…

Error: uncaught exception: Permission denied to call method XMLHttpRequest.open

with 2 comments

If you want to do cross-domain scripting with XMLHttpRequest, e.g. fetching data from a remote location but you’re on a local page or local XUL application (file:///), you need to tell Mozilla/Firefox about that, otherwise you get the infamous error:

Error: uncaught exception: Permission denied to call method XMLHttpRequest.open

Always remember: XMLHttpRequest needs UniversalBrowserRead!

If the page with the XMLHttpRequest is on a http:// URI (on a webserver), it is not possible to fetch data from another domain!!! This is a security measure of Mozilla/Firefox.

cross-domain-xmlhttprequest.html

<script type="text/javascript" language="javascript">

// Error: uncaught exception: Permission denied to call method XMLHttpRequest.open

  var http_request = false;

  function makeRequest(url, parameters) {

   try {
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
   } catch (e) {
    alert("Permission UniversalBrowserRead denied.");
   }

    http_request = false;
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/xml');
    }
    if (!http_request) {
      alert('Cannot create XMLHTTP instance');
      return false;
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('GET', url + parameters, true);
    http_request.send(null);
  }

  function alertContents() {
    if (http_request.readyState == 4) {
      if (http_request.status == 200) {

        var string = http_request.responseText;
      alert(string);

      } else {
        alert('There was a problem with the request.');
      }
    }
  }
  function updateweather() {
    makeRequest('http://www.wunderground.com/auto/rss_full/global/stations/16239.xml', '');
  }
</script>

<input type="button" name="button" value="GET XML"
  onclick="javascript:updateweather();">

Source: http://www.captain.at/howto-ajax-permission-denied-xmlhttprequest.php

Written by borort

January 24, 2009 at 11:55 pm

Posted in Research, Tips

2 Responses

Subscribe to comments with RSS.

  1. I think it will work with only NETSCAPE ENGINE based browser, but not with Internet explorer

    Anjin Pradhan

    May 29, 2009 at 9:49 pm

  2. This does not seem to work in Firefox 3.0. I’ve also tried editing my “prefs.js” file to allow XMLHttpRequests. Anyone have any luck getting passed this in Firefox 3+? Thanks, MartyMayhem

    Marty McGee

    July 21, 2009 at 10:47 am


Leave a Reply