最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
当前位置: 首页 - 科技 - 知识百科 - 正文

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

来源:动视网 责编:小采 时间:2020-11-27 20:28:44
文档

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

javascript跨域请求包装函数与用法示例:本文实例讲述了javascript跨域请求包装函数与用法。分享给大家供大家参考,具体如下:一、源码// 定义AJAX跨域请求的JSON (function(){ if(typeof window.$JSON== 'undefined'){ window.$JSON= {}; }; $JSON._a
推荐度:
导读javascript跨域请求包装函数与用法示例:本文实例讲述了javascript跨域请求包装函数与用法。分享给大家供大家参考,具体如下:一、源码// 定义AJAX跨域请求的JSON (function(){ if(typeof window.$JSON== 'undefined'){ window.$JSON= {}; }; $JSON._a


本文实例讲述了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跨域请求包装函数与用法示例:本文实例讲述了javascript跨域请求包装函数与用法。分享给大家供大家参考,具体如下:一、源码// 定义AJAX跨域请求的JSON (function(){ if(typeof window.$JSON== 'undefined'){ window.$JSON= {}; }; $JSON._a
推荐度:
标签: 使用 js java
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top