JSONP を使用して異なるドメイン間でデータを移動し、JQuery の getJSON メソッドを使用してサーバーを呼び出します。PHP ファイルは適切な形式でデータを返す必要があり、クライアントは JQuery を使用してデータを読み取ることができる必要があります。
サンプルは次のとおりです
。 サーバー側の PHP コード
<?php
header("content-type: application/json");
// Create a generic object.
// Assign it the property 'message' - the feedback message we want the user to see.
$rtnjsonobj->message = "You got an AJAX response via JSONP from another site!";
// Wrap and write a JSON-formatted object with a function call, using the supplied value of parm 'callback' in the URL:
echo $_GET['callback']. '('. json_encode($rtnjsonobj) . ')';
?>
クライアントからデータを取得する
$(document).ready(function() {
$("#jsonpbtn").click(function() {
var surl = "http://www.otherdomain.com/abovepage.php?callback=?";
$.getJSON(surl, function(rtndata) {
alert(rtndata.message);
});
});
});