About this entry
You’re currently reading “Cross Domain XHR with Firefox,” an entry on Web 3.0, 6 Bladed Razors, 7 Minute Abs
- Published:
- 08.30.07 / 5pm
- Category:
- JavaScript, Web Browsers
Bookmark with- del.icio.us
Add to- Digg
Post on
Search on - Technorati
Cross Domain XHR with Firefox
By now know you know that trying to do an XMLHttpRequest (XHR or AJAX) call to a domain that is different from the domain of the hosted JavaScript in Firefox will throw an exception.
Error: uncaught exception: Permission denied to call method XMLHttpRequest.open
If you don't want a history of the past solutions, page down to see the final solution.
The web has solutions to this problem, but most of them involve changing your JavaScript code, which I thought to be less than ideal. A common solution involves setting the UniversalBrowserRead security property in your JavaScript code [Dion Almaer, of Ajaxian fame]:
The problem with that solution (obviously) lies in single browser proprietary JavaScript polluting your code. And you have to set this property inside the scope of any usage (ie: inside your library file that does your AJAX calls and inside your callbacks, etc).
Why can't it just be as easy as Internet Explorer? They just pop-up a little security dialog asking you if you want to allow this access (which is also what the enablePrivilege function does as well).
Another solution involves setting the capability.policy.default.XMLHttpRequest.open preference inside your prefs.js Firefox preference file [Mike Dirolf]. This worked as desired and allowed the AJAX call, but anytime you attempt to access the resulting XML you received a nice exception as well. It turns out this is the solution we wanted, it's just incomplete.
The Final Solution
- Close Firefox. It will overwrite your changes to the prefs.js file if you have it open.
- Optional step: This approach will open up your Firefox security quite a bit, so I'd recommend setting up a separate profile in Firefox to use when testing. It will not pop up a security dialog when a cross-domain AJAX call is made.
- Find your prefs.js file. In Windows, it is typically located in the
C:\Documents and Settings\{YOUR_USERNAME}\ApplicationData\Mozilla\Firefox\Profiles\{YOUR_TEST_USER_PROFILE_ID}\prefs.js - Open it up and add the following lines:
user_pref("capability.policy.default.XMLHttpRequest.open", "allAccess");
user_pref("capability.policy.default.CDATASection.nodeValue", "allAccess");
user_pref("capability.policy.default.Element.attributes", "allAccess");
user_pref("capability.policy.default.Element.childNodes", "allAccess");
user_pref("capability.policy.default.Element.firstChild", "allAccess");
user_pref("capability.policy.default.Element.getElementsByTagName", "allAccess");
user_pref("capability.policy.default.Element.tagName", "allAccess");
user_pref("capability.policy.default.HTMLCollection.length", "allAccess");
user_pref("capability.policy.default.HTMLCollection.item", "allAccess");
user_pref("capability.policy.default.Text.nodeValue", "allAccess");
user_pref("capability.policy.default.XMLDocument.documentElement", "allAccess");
user_pref("capability.policy.default.XMLDocument.getElementsByTagName", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.channel", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.open", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.responseText", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.responseXML", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.send", "allAccess");
user_pref("capability.policy.default.XMLHttpRequest.setRequestHeader", "allAccess");
This code was copied (with the exception of 1 line) from a source repository at [kryogenix.org]
used in jackfield. It wasn't intended to be used for this purpose, but it works.
If you still get Error: uncaught exception: Permission denied to call method _________ errors, you can add the method to your prefs.js. I would appreciate a comment with any commonly used methods not included above. Thanks.
Technorati Tags: Firefox, Cross Domain, AJAX, XHR, JavaScript
subscribe to my blog. You'll save me some bandwidth that way.


14 Comments
Jump to comment form | comments rss [?] | trackback uri [?]