国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

javascript跨域請求包裝函數(shù)與用法示例

2019-11-20 08:36:01
字體:
供稿:網(wǎng)友

本文實(shí)例講述了javascript跨域請求包裝函數(shù)與用法。分享給大家供大家參考,具體如下:

一、源碼

// 定義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請求,再次再調(diào)用時不再進(jìn)行判斷    _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:為回調(diào)函數(shù)所需要傳入的參數(shù)      // 定義頁面中的回調(diào)函數(shù),如例子中的"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;    },    // 生成隨機(jī)JSON回調(diào)函數(shù)    _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);  }})();

二、調(diào)用方式

//調(diào)用方式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);    }  }});

更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《JavaScript中ajax操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript動畫特效與技巧匯總》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)

希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灵宝市| 丹棱县| 额敏县| 丹阳市| 佛学| 龙南县| 韶山市| 麻城市| 双鸭山市| 巴塘县| 汪清县| 龙江县| 沂南县| 商河县| 固始县| 贵州省| 新丰县| 芜湖市| 门源| 隆子县| 麻栗坡县| 潼关县| 巢湖市| 喀什市| 大庆市| 五河县| 上高县| 苍梧县| 黄大仙区| 陆丰市| 江川县| 八宿县| 耒阳市| 张北县| 商南县| 朔州市| 宁陵县| 宁波市| 天全县| 舟曲县| 宜兰市|