原生Ajax至mini-ajax.js

使用详情请访问:https://git.oschina.net/wuquanyao/mini-ajax.js

  1. /*=================================================

  2.  * mini-ajax.js

  3.  * author: wuquanyao

  4.  * email: [email protected]

  5.  * ver: beta

  6.  * date: 2015-10

  7.  *===================For Example===================

  8.  * Ajax.get('request.php',{a:2015,b:'wqyn'},function(response){

  9.  *    console.log(response);

  10.  * },'json');

  11.  *=================================================

  12.  * Ajax.post('request.php',{C:'This is request!',D:'datamap'},function(response){

  13.  * console.log(response);

  14.  * },'json');

  15.  *=================================================

  16.  * Ajax.json('request.php',{xcv:'This is request!',vcd:'datamap'},function(response){

  17.  * console.log(response);

  18.  * });

  19.  *=================================================

  20.  */

  21. var Ajax = {};

  22. (function(Ajax){

  23. Ajax.get = function(url,data,func,dataType){

  24. init(url,data,'GET',func,dataType);

  25. }

  26. Ajax.post = function(url,data,func,dataType){

  27. init(url,data,'POST',func,dataType);

  28. }

  29. Ajax.json = function(url,data,func){

  30. init(url,data,'GET',func,'json');

  31. };

  32. Ajax.upload = function(params){

  33. //do something, not realized, follow up!!

  34. }

  35. function init(url,data,requestType,func,dataType){


  36. if(!url){

  37. alert('Request url can not be empty!');

  38. return false;

  39. }

  40. var dlen = count(data);

  41. if(requestType=='GET'){

  42. if(dlen){

  43. var search = kvalue(data);

  44. url = (url.indexOf('?')==-1)?(url+'?'+search):(url+search);

  45. }

  46. data = null;

  47. }else if(requestType=='POST'){

  48. if(!dlen)

  49. data = null;

  50. else

  51. data = kvalue(data);

  52. }

  53. var xmlHttp        = false,

  54.    requestType    = requestType || 'GET';

  55. dataType       = dataType || 'json';

  56. func           = func || function(param){};

  57. if(window.XMLHttpRequest){

  58. xmlHttp = new XMLHttpRequest();

  59. }else{

  60. try

  61. {

  62. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.5.0");

  63. }catch(e){

  64. try

  65. {

  66. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0");

  67. }catch(e){

  68. try

  69. {

  70. xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");

  71. }catch(e){

  72. try{

  73. xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");

  74. }catch(e){


  75. }

  76. }

  77. }

  78. }

  79. }

  80. if(!xmlHttp){

  81. alert('Your browser does not support Ajax!');

  82. return false;

  83. }

  84. if(xmlHttp.overrideMimeType){ 

  85. xmlHttp.overrideMimeType('text/html');

  86. }

  87. var accept =  {

  88. text: 'text/plain',

  89. html: 'text/html',

  90. xml : 'application/xml,text/xml',

  91. json: 'application/json,text/javascript'

  92. };

  93. //get,post,put,delete

  94. xmlHttp.open(requestType,url,true);

  95. if(requestType == 'POST'){

  96. xmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8');

  97. //xmlHttp.setRequestHeader('Content-type','multipart/form-data');

  98. }

  99. xmlHttp.setRequestHeader('Accept', accept[dataType]+';q=0.9,image/webp,*/*;q=0.8');

  100. xmlHttp.setRequestHeader('Accept-Language', 'zh-cn,zh;q=0.8');

  101. xmlHttp.setRequestHeader('Cache-Control', 'max-age=21600');

  102. xmlHttp.onreadystatechange = function(){

  103. if((xmlHttp.readyState == 4) && xmlHttp.status == 200){

  104. var d = xmlHttp.responseText;

  105. if(dataType == 'json'){

  106. d = JSON.parse(d);

  107. }

  108. func(d);

  109.    };

  110. xmlHttp.send(data);

  111. }

  112. function kvalue(params,sep)

  113. {

  114. var kv  = '';

  115. sep = sep || '&';

  116. for(key in params){

  117. kv+=key+'='+params[key]+sep;

  118. }

  119. return kv.substring(0,kv.length-1);

  120. }

  121. function count(data)

  122. {

  123. var index = 0;

  124. for(i in data){

  125.   index++;

  126. }

  127. return index;

  128. }

  129. })(Ajax);


你可能感兴趣的:(Ajax,原生ajax,mini-ajax)