Premshree Pillai ([info]premshree) wrote,
@ 2005-04-20 12:04:00
Previous Entry  Add to memories!  Tell a Friend!  Next Entry
Current music:The Who - Happy Jack (Live)
Entry tags:apache, cross-domain, cross-domain xmlhttprequest, proxy, xml, xmlhttp, xmlhttprequest

Cross-domain XMLHttpRequest

The other day I was trying some cross-domain XMLHttpRequest stuff. As you probably know, XMLHttpRequest doesn’t work well across domains. (I was testing it locally; with all the coolness, domain restrictions didn’t hit me once.) The solutions is simple—mod_rewrite. I’m not sure if there are docs that talk about this, so I thought it’d be useful to put together this mini how-to. (If you know about the cross-domain issues, you might want to dive to the last section.)

Cross-domain?

Before we get into any of that, following is an example set of functions that would typically form your XMLHttpRequest workhorse »

function getXmlHttpObject(){
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else {
		alert("XMLHttpRequest not supported!");
		return null;
	}
}

function handleHttpResponse() {
	if (http.readyState == 4) {
		results = http.responseText;
		alert(results);
	}
}

function doSomeStuff() {
	var post_arg1 = document.my_form.post_arg1.value;
	var post_arg2 = document.my_form.post_arg2.value;
	var post_url = 'http://yahoo.com/form_do'
	post_data = 'post_arg1=' + post_arg1 + '&post_arg2=' + post_arg2;
	http.open("POST", post_url);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	http.send(post_data);
	http.onreadystatechange = handleHttpResponse;
	return false;
}

var http = getXmlHttpObject();

I’m not going to get into the not-so-gory details of XMLHttpRequest (Ajax, or whatever), there’s tons of places you’ll find good information. (I particularly recommend the last link if you’re new to all this stuff.)

So, getting back on track, the whole point of putting up those lines of code was to illustrate what “cross-domain” means. The last of the three functions that you see is the one that would be called to perform the action. Assume that the above script is within an HTML file, whose URL is, say:

http://premshree.org/form

So, some action (onBlur, onClick, onSubmit, etc.) in form (resides on premshree.org) triggers doSomeStuff(), which in turn makes an XMLHttpRequest request to form_do, which resides on another domain (</tt>yahoo.com</tt>).

Notice the mismatch between the domains of the location of our HTML file (form) and the file that does the action (form_do)? That domain mismatch is precisely what cross-domain is.

XMLHttpRequest is insanely awesome. However, it has domain restrictions. That is, both files—the file where the call is being made, and the file to which the call is being made—need to be within the same domain.

Hold on, cross-domain XMLHttpRequest works... kinda

Actually, cross-domain requests are handled in their own different ways by MSIE and Mozilla. You can do cross-domain requests in MSIE; however, this involves changing its default security settings, or by adding certain hosts to your “trusted hosts” list. Quoting from here:

...
This is how cross-domain security fundamentally works. It's far from a perfect system, but it's simple. Since there is no way to specify which pages trust other pages to access their data, Internet Explorer simply says that if two pages are not in the same domain, they cannot communicate. More precisely, Zone Manager (found on the security tab in Internet Settings) does allow the user to say that a page may access another page, but as you point out, most people leave it set on prompt. You can suggest users add the page to the trusted site zone, or merely say Yes to the dialog box.
...

Mozilla, on the other hand, has the concept of signed scripts. You need to enable one or more of the UniversalBrowser* privileges, depending on the different domains involved in the cross-domain request. For example, if you’re accessing a remote host from your local file system—that is, accessing http:// files from file://—you need to enable UniversalBrowserRead privilege.

Nah, screw it

The reality of the situation is that cross-domain XMLHttpRequest doesn’t work as well as we would want it to on the browsers that we deeply care about, unless, of course, you are insane enough to be willing unsuspecting, naïve users to deal with things like signed scripts and trusted hosts.

Is there a solution?

Yes, thanks to some mod_rewrite magic. All we need is the RewriteRule directive.

The configuration changes need to be made to the configuration file (typically httpd.conf) of the Apache server that serves the file that makes the request (form, in our example; that is, the server that premshree.org runs on). Here are the steps involved:

  • First, Apache must be configured with proxy enabled »

    ./configure --enable-proxy

  • Make sure RewriteEngine is enabled »

    RewriteEngine on

  • Add the following rule »

    RewriteRule ^/form_do$ http://yahoo.com/form_do [P]

    The P flag that you see there indicates a pass-through proxy.

    So now instead of requesting http://yahoo.com/form_do (see bold line in the code; I knew those lines of code would be kinda useful), request for /form_do. So our request code will look like this »

    var post_url = '/form_do';

    That’s it, you’re done.

So there, a solution that cares a damn about the browser you’re using.

Caveat

Note that when you do something like this—dealing with proxies—you need to be very careful about security issues. I’m not terribly good at this, so I’m afraid I might not be able to answer your questions concerning those issues.

Many thanks to Gopal for lot of the information.

Edit [2005-11-21 10:13]: Jason Lewitt has an article on ways to get get around cross-domain XMLHttpRequest. One of them is the pass-through proxy that I discussed here.



(Post a new comment)


[info]madhav
2005-04-20 08:10 am UTC (link)
Ah. I've always wondered about this. Thanks.

(Reply to this)(Thread)

i figured out an easier way
(Anonymous)
2006-06-15 12:03 am UTC (link)
i dont know if this is correct or not...but i was able to make cross domain calls without much hassle....but as long as it works i am good...

i retrived the response as string and then converted it to xml to read it...







Make a request


Title :
Description :




(Reply to this)(Parent)(Thread)

Re: i figured out an easier way
(Anonymous)
2006-06-15 12:05 am UTC (link)
sorry..my earlier post dint post well...just put this code in the body of html and it works...



Make a request


Title :
Description :

(Reply to this)(Parent)

VWeEeZBfzTqaC
(Anonymous)
2008-01-03 01:25 am UTC (link)
XOHgoa hi nice site thx http://peace.com

(Reply to this)(Parent)

TTxHuukjSwRUBBe
(Anonymous)
2008-01-20 10:46 pm UTC (link)
dm82R1 hi nice site thx http://peace.com

(Reply to this)(Parent)

xccMYzMqNLraO
(Anonymous)
2008-01-27 07:45 pm UTC (link)
jRLFod fgbfg7b897fgb0f8g7b8fg8b

(Reply to this)(Parent)

MPpcIagPoODNmlz
(Anonymous)
2008-01-28 12:43 pm UTC (link)
rZ2t50 great site thx http://peace.com

(Reply to this)(Parent)

CzFlUxiyMlqegNbgix
(Anonymous)
2008-01-28 02:00 pm UTC (link)
RNv015 94gbdkli720dv

(Reply to this)(Parent)

PXqUGLVMvWkbYYfARJ
(Anonymous)
2008-01-30 02:36 am UTC (link)
post fantastic is this

(Reply to this)(Parent)

RVAMAVBhgc
(Anonymous)
2008-02-20 04:00 pm UTC (link)
MRXbWD hi good site thx http://peace.com

(Reply to this)(Parent)

MWvjXSRREJtAVfQG
(Anonymous)
2008-03-12 02:22 am UTC (link)
Thanks funny site http://groups.google.com/group/free-alltel-ringtones/web/alltel-ringtones alltel ring tones =[

(Reply to this)(Parent)

mvTOfVXCtWc
(Anonymous)
2008-03-12 04:51 am UTC (link)
It's serious http://groups.google.com/group/airlines-tickets-cheap/web/cheapest-last-minute-tickets last minute tickets ruehi

(Reply to this)(Parent)

xSwLZApvTvttptWsgC
(Anonymous)
2008-03-12 07:13 am UTC (link)
I love this site http://groups.google.com/group/airlines-tickets-cheap/web/cheapest-possible-airline-tickets cheapest airline tickets jtzis

(Reply to this)(Parent)

fhaNniRdSTUwPptqUO
(Anonymous)
2008-03-12 11:36 am UTC (link)
Hello good day http://groups.google.com/group/free-alltel-ringtones/web/alltel-ringtones download alltel ringtones 520042

(Reply to this)(Parent)

jMBmiPugcp
(Anonymous)
2008-03-12 02:39 pm UTC (link)
Gloomy tales http://groups.google.com/group/free-polyphonic-ringtones/web/polyphonic-ringtones free polyphonic ring tones 527

(Reply to this)(Parent)

kxwVHtfSPipb
(Anonymous)
2008-03-23 11:15 am UTC (link)
8nBdGY f76fgb6gb0nb07r70xngm7bx

(Reply to this)(Parent)

YxquncfIkNXI
(Anonymous)
2008-03-23 02:20 pm UTC (link)
jrKFgB fv834fcm92jfd

(Reply to this)(Parent)

hello
(Anonymous)
2008-03-24 12:36 pm UTC (link)
Good site! My site about Kasinos

(Reply to this)(Parent)

hello
(Anonymous)
2008-03-26 01:52 pm UTC (link)
Good site! My site about adipex pills

(Reply to this)(Parent)

meOkcuzcwEzscDBg
(Anonymous)
2008-04-07 08:23 am UTC (link)
M3Q1xJ nice site! http://gov.com

(Reply to this)(Parent)

hello
(Anonymous)
2008-04-14 11:42 am UTC (link)
Good site! My site about vicodin

(Reply to this)(Parent)

Dear inchrist
(Anonymous)
2008-01-14 02:13 am UTC (link)
Dear In Christ,(silva_jikerianip8@yahoo.com

I greet you in the name of our Lord Jesus Christ our Lord, I am Mrs Silva Jikerian from Kuwait. I am married to Mr Alex Jikerian who worked with Kuwait embassy in Ivory Coast for nine years before he died in the year 2004.We were married for eleven years without a child. He died after a brief illness that lasted for only four days. Before his death we were both born again Christians.

Since his death I decided not to remarry or get a child outside my matrimonial home which the Bible is against.When my late husband was alive he deposited the sum of US$9.3million dollars in a Bank here in Abidjan Cote d'Ivoire. Presently, this money is still in bank.Recently, my Doctor told me that I would not last for the next Eight months due to cancer problem.
The one that disturbs me most is my stroke sickness. Having known my condition I decided to donate this fund to a charity organization that will utilize this money the way I am going to instruct herein.
I want an organization that will use this fund for orphanages, schools, churches and widows, propagating the word of God and to endeavor that the house of God is maintained. The Bible made us to understand that "Blessed is the hand that giveth". I took this decision because I don't have any child that will inherit this money and my husband relatives are not Christians and I don't want my husband's efforts to be used by unbelievers.

I don't want a situation where this money will be used in an ungodly way. This is why I am taking this decision. I am not afraid of death hence I know where I am going. I know that I am going to be in the bosom of the Lord. Exodus 14 VS 14 says that "the lord will fight my case and I shall hold my peace". I don't need any telephone communication in this regard because of my health hence the presence of my husband's relatives around me always. I don't want them to know about this development. With God all things are possible. As soon as I receive your reply I shall give you the contact of the Bank here in Abidjan, Cote d'Ivoire.

I will also issue you an authority letter that will prove you the present beneficiary of this fund. I want you and the church to always pray for me because the lord is my shephard. My happiness is that I lived a life of a worthy Christian. Whoever that Wants to serve the Lord must serve him in spirit and Truth. Please always be prayerful all through your life. Any delay in your reply will give me room in sourcing another church/organisation for this same purpose. Please promise me that you will act accordingly as I Stated herein. Hoping to receive your reply.

Remain blessed in the Lord Take care,(silva_jikerianip8@yahoo.com


In Christ's Holy Love
Yours Sister in Christ
Mrs Silva Jikerian

(Reply to this)(Parent)


[info]happysteve
2005-04-20 05:15 pm UTC (link)
Hey there, found this entry through http://del.icio.us/tag/xmlhttprequest
This sounds like a nice simple solution, I love it!
Hope you don't mind if I add you to my friends list.

(Reply to this)


[info]mannu
2005-05-05 10:53 pm UTC (link)
Flash-based apps have the same problem -- they're allowed to access stuff only from the same domain. I get around it by writing a script (PHP) to proxy the request. But your solution seems much easier. Thanks.

(Reply to this)(Thread)


[info]premshree
2005-05-06 06:14 am UTC (link)
Initially I too went with a script-based proxy. However, that would’ve involved every domain user to proxy through a script—one script per domain. I wanted a solution that’d work (kinda) seamlessly.

Everybody I’ve approached with the mod_rewrite solution has told me to be careful about pass-through proxying. Any idea about specific security threats that you see with this approach?

(Reply to this)(Parent)(Thread)

Security
(Anonymous)
2007-08-10 12:57 pm UTC (link)
I too am wanting to use this approach for getting around the limitation of XHR but am waiting until I have a clear understanding of potential risks. All I can think of at the moment is that an excessively permissive pass through rule allows malicious individuals to use you as yet another layer in a denial of service attack.

But this is easy to prevent. Is anyone aware of any serious threats?

(Reply to this)(Parent)

Yet another solution
(Anonymous)
2005-12-16 06:16 pm UTC (link)
I may want to have a look at another XMLHTTP cross-domain solution at ajaxextended.com

(Reply to this)


[info]rockstarling
2006-04-25 04:47 am UTC (link)
Premshree, thanks for this post. Just thought I would let you know that I've just written an article about cross-domain scripting that links here; even though I didn't end up using this particular technique, I still thought your post was interesting and thought others might agree. Thanks again!

(Reply to this)

response cookies
(Anonymous)
2006-05-19 09:52 pm UTC (link)
using this approach I can use the cookies from the response, obviously because the current url does not match the domain in the cookie, any idea.

Thanks,

(Reply to this)

Who listens to what music?
(Anonymous)
2006-12-04 07:10 am UTC (link)
Hello. Good day
Who listens to what music?
I Love songs Justin Timberlake and Paris Hilton

(Reply to this)


[info]alexf
2006-12-18 09:41 pm UTC (link)
Is there any news on this topic? I need a solution without proxy, remote server should see client's IP not my server's IP.

(Reply to this)

(Reply from suspended user)
Free Proxy Access
(Anonymous)
2007-08-27 08:13 am UTC (link)
Your IP address reveals your point of entry to the Internet and can be used to trace your communications back to your ISP, your employer's network, your school, a public terminal.
Use our Free Web Proxy to surf the internet anonymously at http://peak40.com

(Reply to this)

xanax online
(Anonymous)
2008-02-04 03:47 pm UTC (link)
We have the cheapest xanax prices on the net.
Trusted US based pharmacy.
xanax withdrawal
xanax no prescription


buy xanax without a prescription
2mg xanax

cheap xanax
xanax and tinnitus
purchase xanax

(Reply to this)

xanax withdrawal
(Anonymous)
2008-02-09 02:15 pm UTC (link)
Delivery of xanax.
Influence of xanax on your health.
xanax and tinnitus
buy xanax without a prescription


withdrawing off xanax
xanax prescription

phizer xanax
xanax bar picture
xanax exercise equipment

(Reply to this)

Interesting and clean XSS approach
(Anonymous)
2008-02-22 12:05 am UTC (link)
This is a very nice and clean technique. Most all cheap hosting services let users touch the .htaccess and provide mod_rewrite, so this approach is feasible in many cases and simpler than trying to get around the cross-site restriction by relying on server-side scripts.





Rex@PNI offshore call center (http://www.pnicallcenter.com/).

(Reply to this)

Cross domain another way to approach
(Anonymous)
2008-05-03 07:47 am UTC (link)
The enjoyed the above article. It gives information about different cross domain. This is one of the most common approaches. Your script calls your server, your server makes the call to the remote server and then returns the result back to the client. There are some definite advantages to this approach: you have more control over the entire lifecycle.

Pete The Cheap Web Hosting Guru
peteguru@gmail.com
http://www.netresults.ie/web-hosting

(Reply to this)


Create an Account
Forgot your login?
Login w/ OpenID
English • Español • Deutsch • Русский…