Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cross-site XmlHttpRequest in Firefox?

I understand (I think) that XmlHttpRequest objects adhere to the "same-domain" policy. However, I want to create a simple (POC) local html file that downloads XML from a web server and does something with it (let's start with a simple "alert()").

Is it possible at all? Do I need a special Firefox config option?

The server from which I'm trying to download is not under my control (it's actually a Google API).

My simple attempt is the code from Mozilla's "Using XMLHttpRequest" page. It returns an error I don't really understand from the "send" method.

Disclaimer: I'm mainly a C/C++ developer - never done any serious JS programming, never tried using these APIs.

like image 845
noamtm Avatar asked Jan 26 '26 02:01

noamtm


1 Answers

XMLHttpRequest actually adheres to a much stricter implementation of the same domain policy: while you can set the document.domain property to allow JavaScript served from two sub-domains to talk with each other, you can't do that with XMLHttpRequestObject. In your case, going to a completely different domain, you couldn't do that with JavaScript either.

There are a couple of options. First, you can use a reverse proxy to make it appear that the external site is a sub-domain of your site. Take a look at Apache's mod_proxy, in particular ProxyPassReverse

Another alternative is to return data as a JSON object: <script src="foo"> can retrieve whatever data it wants from wherever it wants. Downside of this is that it's not (easily) repeatable (as in multiple requests from same page).

I also recommend that you Google for "google mashups". Most of these live on the "googlemashops.com" domain, which makes implementation a lot easier. A few live outside that domain, and may give you some ideas.

Edit: rather than use the XMLHttpRequest object directly, I recommend going through a third-party library such as prototype.js

like image 138
kdgregory Avatar answered Jan 27 '26 15:01

kdgregory



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!