javascript跨域请求包装函数与用法示例
2019/6/27 21:40:07
本文主要是介绍javascript跨域请求包装函数与用法示例,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
本文实例讲述了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); } } });
更多关于JavaScript相关内容感兴趣的读者可查看本站专题:《JavaScript中ajax操作技巧总结》、《JavaScript切换特效与技巧总结》、《JavaScript查找算法技巧总结》、《JavaScript动画特效与技巧汇总》、《JavaScript错误与调试技巧总结》、《JavaScript数据结构与算法技巧总结》、《JavaScript遍历算法与技巧总结》及《JavaScript数学运算用法总结》
希望本文所述对大家JavaScript程序设计有所帮助。
这篇关于javascript跨域请求包装函数与用法示例的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-30React Native常用组件-点击组件
- 2024-05-30uniapp+vue3+uv-ui手机端后台OA管理模板
- 2024-05-29Python网络爬虫的时候json=就是让你少写个json.dumps()
- 2024-05-27React Native常用组件-展示组件
- 2024-05-27React Native常用组件-列表组件
- 2024-05-09vue3开发前端表单缓存自定义指令,移动端h5必备插件
- 2024-05-09React Hooks在class组件中的使用方式
- 2024-03-30[OIDC in Action] 2. 基于OIDC(OpenID Connect)的SSO(纯JS客户端)
- 2024-03-29terraform jsonencode
- 2024-03-13vuex-persist