ホームページ  >  記事  >  ウェブフロントエンド  >  Jquery_jquery に基づくクロスドメイン データ送信 (JSONP)

Jquery_jquery に基づくクロスドメイン データ送信 (JSONP)

WBOY
WBOYオリジナル
2016-05-16 18:09:391156ブラウズ

バックエンド:

コードをコピー コードは次のとおりです:

$json_str = json_encode(array("ddd"=>"11111111"));
echo $_GET['ja'].'('.$json_str.')';

フロントエンド:


$.getJSON(' http://www.liushan.cn/test.php?ja=?',function (json){
alert(json);
}); >
純粋な JS 実装 (JSONP):



コードをコピー
コードは次のとおりです: //server return aa({' ddd':'ddd'}) コールバック関数名: $_GET['callback'] //author:lonely
(function(w){
function getjson(){}
getjson.prototype.set=function(url,callback,callbackname){
this.callfn=callbackname||'urlcallback'
this.url=url "?callback=" this.callfn;
try{
eval(this.callfn "=function(data){n"
"callback(data);n"
'delete ' this.callfn ';}' );
}catch(e) {return;}
this.request();
}
getjson.prototype.request=function(){
var script=document.createElement("script ");
script.src=this.url;
script.onload = script.onreadystatechange = function() {
if(this.readyState === "ロード済み " || this.readyState === "complete"){
load=true;
script.onload = script.onreadystatechange=null; >};
var head=document.getElementsByTagName("head")[0];
head.insertBefore(script,head.firstChild);
w.getjson=getjson; >})(window)
/ /Use DOME
new getjson().set("http://www.test.cn/test.php",function(data){
alert(data) .ddd);
});


別のコード: getScript




コードは次のとおりです:


jQuery.getScript("http://dev.jquery.com/view/trunk/plugins/color/jquery.color.js", function(){
$("#go").click(function( ){
$(".block").animate( { 背景色: 'ピンク' }, 1000)
.animate( { 背景色: '青' } , 1000);
});//CHM ヘルプ ファイルの例
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。