N久前写的一个JSONP请求方法
2011-09-08 11:43 sniper007 阅读(190) 评论(0) 编辑 收藏 举报function getJSON(url, params, callback, charset){
var w = window,
query = url.indexOf("?") == -1 ? "?" : "&",
head,
callback = isFunction(params) ? params : callback,
charset = charset || "utf-8"
function getTimeVer(){
return new Date().getTime();
}
function isFunction(obj){
return Object.prototype.toString.call(obj) === "[object Function]";
}
function load(url){
var script = document.createElement('script'),
done = false;
script.src = url;
script.async = true;
script.charset = charset;
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) {
done = true;
script.onload = script.onreadystatechange = null;
if ( script && script.parentNode ) {
script.parentNode.removeChild( script );
}
}
};
if ( !head ) {
head = document.getElementsByTagName('head')[0];
}
head.appendChild( script );
}
if(typeof params === "string"){
params = params || "";
query += "&";
}else{
params = params || {};
for (var key in params ) {
if ( params.hasOwnProperty(key) ) {
query += encodeURIComponent(key) + "=" + encodeURIComponent(params[key]) + "&";
}
}
}
var jsonVar = "jsonp" + getTimeVer();
w[jsonVar] = function(data){
callback.call(null, data);
try{
delete w[jsonVar];
}catch(e){
}
w[jsonVar] = null;
};
load(url + query + "callback=" + jsonVar);
}
document.getElementById("click").onclick = function(){
getJSON("http://www-0f3d8c090ce:8080/foundation/servlet/JsonCallBack", function(data){
alert(data.name);
});
};
var w = window,
query = url.indexOf("?") == -1 ? "?" : "&",
head,
callback = isFunction(params) ? params : callback,
charset = charset || "utf-8"
function getTimeVer(){
return new Date().getTime();
}
function isFunction(obj){
return Object.prototype.toString.call(obj) === "[object Function]";
}
function load(url){
var script = document.createElement('script'),
done = false;
script.src = url;
script.async = true;
script.charset = charset;
script.onload = script.onreadystatechange = function() {
if ( !done && (!this.readyState || this.readyState === "loaded" || this.readyState === "complete") ) {
done = true;
script.onload = script.onreadystatechange = null;
if ( script && script.parentNode ) {
script.parentNode.removeChild( script );
}
}
};
if ( !head ) {
head = document.getElementsByTagName('head')[0];
}
head.appendChild( script );
}
if(typeof params === "string"){
params = params || "";
query += "&";
}else{
params = params || {};
for (var key in params ) {
if ( params.hasOwnProperty(key) ) {
query += encodeURIComponent(key) + "=" + encodeURIComponent(params[key]) + "&";
}
}
}
var jsonVar = "jsonp" + getTimeVer();
w[jsonVar] = function(data){
callback.call(null, data);
try{
delete w[jsonVar];
}catch(e){
}
w[jsonVar] = null;
};
load(url + query + "callback=" + jsonVar);
}
document.getElementById("click").onclick = function(){
getJSON("http://www-0f3d8c090ce:8080/foundation/servlet/JsonCallBack", function(data){
alert(data.name);
});
};