ܻؤ1顪30奵꡼ޥܥ

ITȤƯ30奵꡼ޥΥ饤ߥ󥰵Ͽ

JSONP

դjsonpȤȻפä ֤ܤΥɥɲϤ­ʤ̵̤˻֤ä
˺ʤ褦˥⡣

$.ajax({
    type: "GET",
    url: "http://sample.com/",
    data: {
      "param": "hogehoge",
    },
    dataType: "jsonp",
    jsonp: "callBack",
    success: function(data) {
      /* success */
    },
    complete: function(data) {
      /* complete */
    }
});

dataTypejsonpˤjQuerycallbackѥ᡼ưƤ롣

callback=jsonp1234391216435ߤʡ

jsonpץդȢcallbackʬꤷ̾ˤǤ롣
¦APIˤäcallbackΥѥ᡼̾Ƥ礬ΤǤȤ˻Ȥ


callbackؿꤷdataץ˽񤯡

data: { "callback" : "hoge" }

callbackؿꤷʤjQueryǰʲΥɤ¹Ԥ롣
window[ 'jsonp1242391216435' ] = function(tmp){
  data = tmp;
  success();
  complete();
  // ١쥯Ƚ
  window[ jsonp ] = undefined;
  try{ delete window[ jsonp ]; } catch(e){}
  if ( head )
  head.removeChild( script );
}


¦ϤʤΤǤȤꤢǡ֤Ƥߤ
֤ǡϤˡ
<?php
$data = array("a" => "hoge", "b" => "foo", "c" => "bar");
$json = json_encode($data);
$callback = htmlspecialchars(strip_tags($_GET["callback"]));
echo $callback . "(" . $json . ")";
?>
    Υȥ꡼ϤƤʥ֥åޡɲ

ΥڡΥȥåץ