Cross-site AJAX using jQuery

JSONP will allow you to do cross-site calls. See jQuery docs on that matter.

The concept is simple: instead of doing a normal Ajax call, jQuery will append a <script> tag to your <head>. In order for this to work, your JSON data needs to be wrapped in a
function call.

Your server needs to send information in such way (PHP example):

$json = json_encode($data);
echo $_GET['jsonp_callback'] . '(' . $json . ');';

Then, you can use jQuery to fetch that information:

$.ajax({
  dataType: 'jsonp',
  jsonp: 'jsonp_callback',
  url: 'http://myotherserver.com/getdata',
  success: function () {
    // do stuff
  },
});

More information is available here: What is JSONP?

Leave a Comment