Cross-Domain AJAX Requests

Browsers prevent standard AJAX calls across domains for security reasons, but you can make JSONP calls instead. If you’re using jQuery you just need to add ‘callback=?’ to the url:

$.getJSON('', function(data)
// do stuff
} );

Then setup the request handler to prepend the callback parameter to the response and wrap it in quotes:

echo $_REQUEST['callback'] .'('. json_encode($results) .')';

If you’re using Firebug to monitor the call, it won’t show up on the Console tab anymore. Instead it will be in the Net tab.

Leave a Reply

Your email address will not be published. Required fields are marked *