首頁 >php教程 >php手册 >javascript原生ajax之mini-ajax.js

javascript原生ajax之mini-ajax.js

WBOY
WBOY原創
2016-06-07 11:36:401518瀏覽

原生javascript实现的ajax方法,如:Ajax.post();Ajax.get();Ajax.json();Ajax.upload();
使用详情请访问:https://git.oschina.net/wuquanyao/mini-ajax.js
/*=================================================<br>  * mini-ajax.js<br>  * author: wuquanyao<br>  * email: wqynqa@163.com<br>  * ver: beta<br>  * date: 2015-10<br>  *===================For Example===================<br>  * Ajax.get('request.php',{a:2015,b:'wqyn'},function(response){<br>  *    console.log(response);<br>  * },'json');<br>  *=================================================<br>  * Ajax.post('request.php',{C:'This is request!',D:'datamap'},function(response){<br>  *    console.log(response);<br>  * },'json');<br>  *=================================================<br>  * Ajax.json('request.php',{xcv:'This is request!',vcd:'datamap'},function(response){<br>  *    console.log(response);<br>  * });<br>  *=================================================<br>  */<br> var Ajax = {};<br> (function(Ajax){<br>     <br>     Ajax.get = function(url,data,func,dataType){<br>         init(url,data,'GET',func,dataType);<br>     }<br>     <br>     Ajax.post = function(url,data,func,dataType){<br>         init(url,data,'POST',func,dataType);<br>     }<br>     <br>     Ajax.json = function(url,data,func){<br>         init(url,data,'GET',func,'json');<br>     };<br>     Ajax.upload = function(params){<br>         //do something, not realized, follow up!!<br>     }<br>     function init(url,data,requestType,func,dataType){<br> <br>         if(!url){<br>             alert('Request url can not be empty!');<br>             return false;<br>         }    <br>         <br>         var dlen = count(data);<br>         if(requestType=='GET'){<br>             if(dlen){<br>                 var search = kvalue(data);<br>                 url = (url.indexOf('?')==-1)?(url+'?'+search):(url+search);<br>             }<br>             data = null;<br>         }else if(requestType=='POST'){<br>             if(!dlen)<br>             data = null;<br>             else<br>             data = kvalue(data);<br>         }<br>         <br>         var xmlHttp        = false,<br>             requestType    = requestType || 'GET';<br>             dataType       = dataType || 'json';<br>             func           = func || function(param){};<br>         <br>         if(window.XMLHttpRequest){<br>             xmlHttp = new XMLHttpRequest();<br>         }else{<br>             try<br>             {<br>                 xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");<br>             }catch(e){<br>                 try<br>                 {<br>                     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");<br>                 }catch(e){<br>                     try<br>                     {<br>                         xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");<br>                     }catch(e){<br>                         try{<br>                             xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");<br>                         }catch(e){<br> <br>                         }<br>                     }    <br>                 }<br>             }            <br>         }<br>         <br>         if(!xmlHttp){<br>             alert('Your browser does not support Ajax!');<br>             return false;<br>         }<br>         if(xmlHttp.overrideMimeType){ <br>             xmlHttp.overrideMimeType('text/html');<br>         }<br>         var accept =  {<br>             text: 'text/plain',<br>             html: 'text/html',<br>             xml : 'application/xml,text/xml',<br>             json: 'application/json,text/javascript'<br>         };<br>         //get,post,put,delete<br>         xmlHttp.open(requestType,url,true);<br>         if(requestType == 'POST'){<br>             xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8');<br>             //xmlHttp.setRequestHeader('Content-type','multipart/form-data');<br>         }<br>         xmlHttp.setRequestHeader('Accept', accept[dataType]+';q=0.9,image/webp,*/*;q=0.8');<br>         xmlHttp.setRequestHeader('Accept-Language', 'zh-cn,zh;q=0.8');<br>         xmlHttp.setRequestHeader('Cache-Control', 'max-age=21600');<br>         xmlHttp.onreadystatechange = function(){<br>             if((xmlHttp.readyState == 4) && xmlHttp.status == 200){<br>                 var d = xmlHttp.responseText;<br>                 if(dataType == 'json'){<br>                     d = JSON.parse(d);<br>                 }<br>                 func(d);<br>             };<br>         } <br>         xmlHttp.send(data);<br>     }<br>     <br>     function kvalue(params,sep)<br>     {<br>         var kv  = '';<br>             sep = sep || '&';<br>         for(key in params){<br>             kv+=key+'='+params[key]+sep;<br>         }<br>         return kv.substring(0,kv.length-1);<br>     }<br>     <br>     function count(data)<br>     {<br>         var index = 0;<br>         for(i in data){<br>            index++;<br>         }<br>         return index;<br>     }<br>     <br> })(Ajax);

附件 QQ截图20151027101029.png ( 115.41 KB 下载:24 次 )

AD:真正免费,域名+虚机+企业邮箱=0元

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn