您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

javascript跨域请求包装函数与用法示例

2024/5/10 11:17:27发布23次查看
本文实例讲述了javascript跨域请求包装函数与用法。分享给大家供大家参考,具体如下:
一、源码
// 定义ajax跨域请求的json (function(){ if(typeof window.$json== 'undefined'){ window.$json= {}; }; $json._ajax = function(config){ config = config[0] || {}; this.url = config.url || ''; this.type = config.type || 'xhr'; this.method = (this.type == 'json') ? 'get' : config.method.touppercase() || 'get'; this.param = config.param || null; this.callback = config.callback || {}; this.xhr = null; if(typeof window._$json_callback == 'undefined'){ window._$json_callback = {}; } this._createrequest(); }; $json._ajax.prototype = { // 缓存xhr请求,再次再调用时不再进行判断 _createxhr : function(){ var methods = [ function(){ return new xmlhttprequest(); }, function(){ return new activexobject('msxml2.xmlhttp'); }, function(){ return new activexobject('microsoft.xmlhttp'); } ]; for(var i = 0, l = methods.length; i < l; i++){ try{ methods[i](); }catch(e){ continue; } this._createxhr = methods[i]; return methods[i](); } }, // 建立xhr请求 _createrequest : function(){ return (this.type == 'json') ? this._setjsonrequest() : this._setxhrrequest(); }, _setxhrrequest : function(){ var _this = this; var param = ''; for(var i in this.param){ if(param == ''){ param = i+'='+this.param[i]; }else{ param+= '&'+i+'='+this.param[i]; } } this.xhr = this._createxhr(); this.xhr.onreadystatechange = function(){ if(_this.xhr.readystate == 4 && _this.xhr.status == 200){ _this.callback.success(_this.xhr.responsetext); }else{ _this.callback.failure('重新操作'); } }; this.xhr.open(this.method, this.url, true); this.xhr.setrequestheader("content-type","application/x-www-form-urlencoded; charset=utf-8"); this.xhr.send(param); }, // 建立json请求 _setjsonrequest : function(){ var head = document.getelementsbytagname('head')[0]; var script = document.createelement('script'); var fun = this._setrandomfun(); var _this = this; var param = ''; for(var i in this.param){ if(param == ''){ param = i+'='+this.param[i]; }else{ param+= '&'+i+'='+this.param[i]; } } script.type = 'text/javascript'; script.charset = 'utf-8'; if(head){ head.appendchild(script); }else{ document.body.appendchild(script); } // data:为回调函数所需要传入的参数 // 定义页面中的回调函数,如例子中的"jsonpcallback()"方法 window._$json_callback[fun.id] = function(data){ _this.callback.success(data); settimeout(function(){ delete window._$json_callback[fun.id]; script.parentnode.removechild(script); }, 100); }; script.src = this.url+'?callback='+fun.name+'&'+param; }, // 生成随机json回调函数 _setrandomfun : function(){ var id = ''; do{ id = '$json'+math.floor(math.random()*10000); }while(window._$json_callback[id]) return{ id : id, name : 'window._$json_callback.'+id } } }; window.$json.ajax = function(){ return new $json._ajax(arguments); } })();
二、调用方式
//调用方式 var ajax = new $json.ajax({ url : 'http://www.sina.com/api', type : 'json', method : 'get', param: { bar: true }, callback : { success : function(data){ console.log( '55668',data); }, failure : function(error){ alert(errow); } } });
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product