Server:
1 2 3 |
header('Cache-Control: no-cache, must-revalidate'); header('Content-type: application/javascript;charset=utf-8'); echo 'jsonp_callback(' . json_encode($data) . ')'; |
Client:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
(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: