Wednesday, April 18, 2012

"Same Domain Policy" limitation of jQuery

The same domain policy prevents javascripts hosted on one domain from accessing data from a remote domain. This is for security purpose. So when use jQuery, you can not directly access a web service from a remote server.

The solution is to create a serverside script (for example, in php, as shown below) on your domain, which will serve as a proxy, get the data from the remote domain, and then output it. For example:

<?php
readfile('http://remote_server.com/service?param=' . urlencode($_GET['param']);
?>

[1] Getting data from remote URL using jquery
[2] Cross-domain requests with jQuery

No comments:

Blog Archive

Followers