| Cross Site/Domain AJAX Posted by: Jordan in JavaScript, CodeCall on Jun 16, 2008 |
I've been working with CCIT a bit lately and ran into an AJAX problem that prevented my script from working. I'm unsure how to finish the project now since the original intention was to allow other websites (on other domains) to display the CodeCall Inline Text feature.
AJAX - Cross Domain AJAX
Using AJAX from one site to another doesn't work well. The problem is a security issue that prevents the HttpObject from accessing a URI different than the executing URI. For example, I couldn't access http://www.codecall.net/somescript.php from http://blog.codecall.net.
FireFox will throw this error:

IE Simply shows this:

Fixing the Problem
I didn't find any good solutions for cross site/domain AJAX fetching. I found some code on a site that would allow FireFox to work (although it didn't work):
Using the above code instead of a static URL (http://www.codecall.net) the AJAX will fetch the current domain script. For instance, if you are browsing http://blog.codecall.net, CCIT will pick up http://blog.codecall.net/script.php. At the same time if you are browsing http://www.codecall.net CCIT will fetch http://www.codecall.net/script.php.
This is just a temporary fix, a quick fix.
The Real Solution
Looking for a fix I came across JSON (JavaScript Object Notation) and Dynamic Script Tags. Using this I will be able to accomplish where AJAX fails (across domains) but it will mean a rewrite of certain CCIT functions and could take a while. Look forward to a JSON/Dynamic Script Tag blog (one I learn it and actually use it).
AJAX - Cross Domain AJAX
Using AJAX from one site to another doesn't work well. The problem is a security issue that prevents the HttpObject from accessing a URI different than the executing URI. For example, I couldn't access http://www.codecall.net/somescript.php from http://blog.codecall.net.
FireFox will throw this error:

IE Simply shows this:

Fixing the Problem
I didn't find any good solutions for cross site/domain AJAX fetching. I found some code on a site that would allow FireFox to work (although it didn't work):
try {
user_pref("capability.policy.XMLHttpRequestToAnySite.XMLHttpRequest.open", "allAccess");
user_pref("capability.policy.XMLHttpRequestToAnySite.sites", "http://www.codecall.net");
user_pref("capability.policy.policynames", "XMLHttpRequestToAnySite");
} catch (e) {
alert("Permission UniversalBrowserRead denied.");
}
var url = 'http://'+location.host+'/script.php';
Using the above code instead of a static URL (http://www.codecall.net) the AJAX will fetch the current domain script. For instance, if you are browsing http://blog.codecall.net, CCIT will pick up http://blog.codecall.net/script.php. At the same time if you are browsing http://www.codecall.net CCIT will fetch http://www.codecall.net/script.php.
This is just a temporary fix, a quick fix.
The Real Solution
Looking for a fix I came across JSON (JavaScript Object Notation) and Dynamic Script Tags. Using this I will be able to accomplish where AJAX fails (across domains) but it will mean a rewrite of certain CCIT functions and could take a while. Look forward to a JSON/Dynamic Script Tag blog (one I learn it and actually use it).