JSONP Example

Server:

header('Cache-Control: no-cache, must-revalidate');
header('Content-type: application/javascript;charset=utf-8');
echo 'jsonp_callback(' . json_encode($data) . ')';

Client:

(function() {
  var api = "http://www.example.com/result.php?form=191";
  $.getJSON(api + '&callback=?', jsonp_callback);
})();

//Function name should be the same as the one in server response
function jsonp_callback(data) {
    //console.log(data);
    for(var i = 0; i < data.length; i++){
        var option = data[i];
        var $n = $('#option_' + option.id);
        $n.html(option.count);
    }
}

Reference:

  1. http://www.dotblogs.com.tw/topcat/archive/2011/09/21/37024.aspx