基于Jquery的跨域傳輸數(shù)據(jù)(JSONP)
更新時間:2011年03月10日 13:02:32 作者:
基于Jquery的跨域傳輸數(shù)據(jù)(JSONP) ,需要的朋友可以參考下。
后端:
<?php
$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實現(xiàn)(JSONP):
//server return aa({'ddd':'ddd'}) callback function name: $_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();
delete this.url;
}
getjson.prototype.request=function(){
var script=document.createElement("script");
script.src=this.url;
var load=false;
script.onload = script.onreadystatechange = function() {
if(this.readyState === "loaded" || 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)
//使用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( { backgroundColor: 'pink' }, 1000)
.animate( { backgroundColor: 'blue' }, 1000);
});
});//CHM幫助文件示例
復制代碼 代碼如下:
<?php
$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實現(xiàn)(JSONP):
復制代碼 代碼如下:
//server return aa({'ddd':'ddd'}) callback function name: $_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();
delete this.url;
}
getjson.prototype.request=function(){
var script=document.createElement("script");
script.src=this.url;
var load=false;
script.onload = script.onreadystatechange = function() {
if(this.readyState === "loaded" || 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)
//使用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( { backgroundColor: 'pink' }, 1000)
.animate( { backgroundColor: 'blue' }, 1000);
});
});//CHM幫助文件示例
您可能感興趣的文章:
- jQuery使用JSONP實現(xiàn)跨域獲取數(shù)據(jù)的三種方法詳解
- 通過jsonp獲取json數(shù)據(jù)實現(xiàn)AJAX跨域請求
- JavaScript用JSONP跨域請求數(shù)據(jù)實例詳解
- AJAX跨域請求之JSONP獲取JSON數(shù)據(jù)
- 使用jsonp實現(xiàn)跨域獲取數(shù)據(jù)實例講解
- AngularJS實現(xiàn)的JSONP跨域訪問數(shù)據(jù)傳輸功能詳解
- Ajax的jsonp方式跨域獲取數(shù)據(jù)的簡單實例
- jsonp跨域請求數(shù)據(jù)實現(xiàn)手機號碼查詢實例分析
- AJAX跨域請求JSONP獲取JSON數(shù)據(jù)的實例代碼
- jsonp跨域獲取數(shù)據(jù)的基礎教程
相關文章
jQuery實現(xiàn)對象轉(zhuǎn)為url參數(shù)的方法
這篇文章主要介紹了jQuery實現(xiàn)對象轉(zhuǎn)為url參數(shù)的方法,涉及jQuery針對字符串的遍歷與轉(zhuǎn)換操作相關技巧,需要的朋友可以參考下2017-01-01jQuery實現(xiàn)表單動態(tài)添加與刪除數(shù)據(jù)操作示例
這篇文章主要介紹了jQuery實現(xiàn)表單動態(tài)添加與刪除數(shù)據(jù)操作,涉及jQuery事件響應及頁面元素動態(tài)操作相關實現(xiàn)技巧,需要的朋友可以參考下2018-07-07jQuery通過點擊行來刪除HTML表格行的實現(xiàn)示例
從一個HTML表使用一些時髦的效果,只要按一下該行,改行即可被刪除,這個示例比較簡單,新手朋友們可以學習下2014-09-09