成熟丰满熟妇高潮XXXXX,人妻无码AV中文系列久久兔费 ,国产精品一国产精品,国精品午夜福利视频不卡麻豆

您好,歡迎來到九壹網(wǎng)。
搜索
您的當前位置:首頁javascript跨域請求包裝函數(shù)與用法示例

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

來源:九壹網(wǎng)

本文實例講述了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請求,再次再調用時不再進行判斷
 _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:為回調函數(shù)所需要傳入的參數(shù)
 // 定義頁面中的回調函數(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;
 },
 // 生成隨機JSON回調函數(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);
 }
})();

二、調用方式

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

Copyright ? 2019- 91gzw.com 版權所有 湘ICP備2023023988號-2

違法及侵權請聯(lián)系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市萬商天勤律師事務所王興未律師提供法律服務