Pseudo-AJAX: Circumventing the Same-Origin Policy Using the Tag
A friend of mine asked me to build an AJAX shipping calculator for his e-commerce site. I accepted the project, thinking that his request would be easy to fulfill. I soon learned that his site was hosted by a third party e-commerce hosting company (StoresOnline) that does not (a) allow FTP access to his site and (b) process PHP or any other server-side scripting language. Using AJAX was going to be next to impossible. The reason it was going to be so hard is the same-origin policy.
The same-origin policy is incorporated into every major browser and prevents a script loaded from one site of origin from communicating with a document loaded from another site of origin.
I needed the shipping calculator to make a request to the R+L Carriers quote service, located at: http://www.rlcarriers.com/b2brateparam.asp. Same-origin policy says I can’t make a direct AJAX request (that will return a response) to any domain and port other than the one that the site content is on.
Normally circumventing the same-origin policy is a breeze, you just use a proxy on your domain to make the request. For example, you would use a URL like this in your AJAX request:
var url = '/proxy?url=' + encodeURIComponent('http://www.rlcarriers.com/b2brateparam.asp?weight1=1000')
See this Prototype JS documentation for further details.
I usually use a PHP proxy on my server to make requests to other domains. Unfortunately, StoresOnline will not allow me to use a proxy script on my friend’s domain. I called one of the developers I know at the company and he said he’d probably be fired if he put one up for me.
There is a solution to every problem.
Turns out the <script> tag doesn’t care about same-origin policy. You can specify any domain you want in the ’src’ attribute of the script tag. StoresOnline allows me to put custom script tags into the checkout page.
Here’s kind of what it looks like:
<script src="http://www.foreigndomain.com/checkout.js" language="javascript"></script>
checkout.js has a function in it that runs code that looks like this, using the Prototype JavaScript library:
// Create the new script element and assign the a src attribute
var newScript = new Element('script', { 'src': 'http://www.foreigndomain.com/calculate-shipping.php?weight1=1000'});
// Put the new script into the page
document.body.appendChild(newScript);
When the function that contains this code is activated, a new script tag is created and added to the page on the fly. You can throw whatever variables from the current page into the parameters of your script tag src attribute. In the instance above I grab a zip code from the checkout page and pass it along with the weight. calculate-shipping.php, hosted on one of my domains, then makes the request to R+L Carriers and gets the shipping cost. All that is left is to have calculate-shipping.php echo out some JavaScript for the client to process. This is PHP echoing JavaScript:
// Process the shipping cost and put it into a variable called $shippingCharges
// Echo it out
echo "
$('shippingCharge').value = {$shippingCharges}; // This will update a form field on the checkout page with the estimated shipping cost
";
Turns out it the technique works like a charm. The purpose of all of the code above is just to explain the concept of using the <script> tag to circumvent the same-origin policy. The actual code that I am using has a lot more checks to make things a little more secure. You can check it out by visiting my friend’s e-commerce site at http://www.emergencymre.com/. Add something to your cart and go to the checkout page to see it in action.
Comments welcome!
Just found this. I liked it.
At school:
“Indicators of critical thinking can be identified through the types of activities in which the teacher engages the students. For example:
On a much different note—though in my relatively average mind, related (and in my native tongue of ENGLISH =o) )—you can spend 5 minutes with a child and realize that having questions is instinctive. You’re right about adults not having near as many. At least not verbalizing them. Somewhere throughout their formative years we squelch the curiosity, or don’t provide a learning environment conducive of asking them. Or don’t provide an atmosphere where they feel comfortable asking them.
Yeah - I think people struggle with similar issues, but on slightly different levels… you are challenged by trying to simultaneously grapple with running a 1-parameter logistic model AND the assumptions underlying perception and epistemology
- and others find it difficult to chew gum AND walk at the same time.
Now you got me thinking… never good when I’m trying to run a 1-parameter logistic model.
I’m not sure of the linearity between priorities, assumptions, and questions, but they are surely inter-related. Your examples show why assumptions may precede priorities. Perhaps they’re the same construct, but we have yet to unify them. Or perhaps I’m dead wrong and you’re right.
You’re right, people are skeptical of those with power - and usually for good reason and from lots of previous experience.
I do think America is misunderstood a lot - I’ve noticed how often here in Africa people confuse American culture and American media. And unfortunately the media is a very poor reflection of the culture.
Strange as it may seem, even in the remotest parts of Africa I find American media - and usually in Chinese packaging. I wonder if the artist ever imagined in their wildest dreams their music/film/etc would be played in the contexts it is.