Advertisement
Guest User

Untitled

a guest
Dec 16th, 2018
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. /*
  2. * xhr.js - XMLHttpRequest helper class
  3. * (c) 2008-2010 Jo-Philipp Wich
  4. */
  5.  
  6. XHR = function()
  7. {
  8. this.reinit = function()
  9. {
  10. if( window.XMLHttpRequest ) {
  11. this._xmlHttp = new XMLHttpRequest();
  12. }
  13. else if( window.ActiveXObject ) {
  14. this._xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
  15. }
  16. else {
  17. alert("xhr.js: XMLHttpRequest is not supported by this browser!");
  18. }
  19. }
  20.  
  21. this.busy = function() {
  22. switch( this._xmlHttp.readyState )
  23. {
  24. case 1:
  25. case 2:
  26. case 3:
  27. return true;
  28.  
  29. default:
  30. return false;
  31. }
  32. }
  33.  
  34. this.abort = function() {
  35. if( this.busy() )
  36. this._xmlHttp.abort();
  37. }
  38.  
  39. this.get = function(url,data,callback)
  40. {
  41. this.reinit();
  42.  
  43. var xhr = this._xmlHttp;
  44. var code = this._encode( data );
  45.  
  46. url = location.protocol + '//' + location.hostname +
  47. ( location.port ? ':' + location.port : '' ) + url;
  48.  
  49. if( code )
  50. if( url.substr(url.length-1,1) == '&' )
  51. url += code;
  52. else
  53. url += '?' + code;
  54.  
  55. xhr.open( 'GET', url, true );
  56.  
  57. xhr.onreadystatechange = function()
  58. {
  59. if( xhr.readyState == 4 ) {
  60. var json = null;
  61. if( xhr.getResponseHeader("Content-Type") == "application/json" ) {
  62. try {
  63. json = eval('(' + xhr.responseText + ')');
  64. }
  65. catch(e) {
  66. json = null;
  67. }
  68. }
  69.  
  70. callback( xhr, json );
  71. }
  72. }
  73.  
  74. xhr.send( null );
  75. }
  76.  
  77. this.post = function(url,data,callback)
  78. {
  79. this.reinit();
  80.  
  81. var xhr = this._xmlHttp;
  82. var code = this._encode( data );
  83.  
  84. xhr.onreadystatechange = function()
  85. {
  86. if( xhr.readyState == 4 )
  87. callback( xhr );
  88. }
  89.  
  90. xhr.open( 'POST', url, true );
  91. xhr.setRequestHeader( 'Content-type', 'application/x-www-form-urlencoded' );
  92. xhr.setRequestHeader( 'Content-length', code.length );
  93. xhr.setRequestHeader( 'Connection', 'close' );
  94. xhr.send( code );
  95. }
  96.  
  97. this.cancel = function()
  98. {
  99. this._xmlHttp.onreadystatechange = function(){};
  100. this._xmlHttp.abort();
  101. }
  102.  
  103. this.send_form = function(form,callback,extra_values)
  104. {
  105. var code = '';
  106.  
  107. for( var i = 0; i < form.elements.length; i++ )
  108. {
  109. var e = form.elements[i];
  110.  
  111. if( e.options )
  112. {
  113. code += ( code ? '&' : '' ) +
  114. form.elements[i].name + '=' + encodeURIComponent(
  115. e.options[e.selectedIndex].value
  116. );
  117. }
  118. else if( e.length )
  119. {
  120. for( var j = 0; j < e.length; j++ )
  121. if( e[j].name ) {
  122. code += ( code ? '&' : '' ) +
  123. e[j].name + '=' + encodeURIComponent( e[j].value );
  124. }
  125. }
  126. else
  127. {
  128. code += ( code ? '&' : '' ) +
  129. e.name + '=' + encodeURIComponent( e.value );
  130. }
  131. }
  132.  
  133. if( typeof extra_values == 'object' )
  134. for( var key in extra_values )
  135. code += ( code ? '&' : '' ) +
  136. key + '=' + encodeURIComponent( extra_values[key] );
  137.  
  138. return(
  139. ( form.method == 'get' )
  140. ? this.get( form.getAttribute('action'), code, callback )
  141. : this.post( form.getAttribute('action'), code, callback )
  142. );
  143. }
  144.  
  145. this._encode = function(obj)
  146. {
  147. obj = obj ? obj : { };
  148. obj['_'] = Math.random();
  149.  
  150. if( typeof obj == 'object' )
  151. {
  152. var code = '';
  153. var self = this;
  154.  
  155. for( var k in obj )
  156. code += ( code ? '&' : '' ) +
  157. k + '=' + encodeURIComponent( obj[k] );
  158.  
  159. return code;
  160. }
  161.  
  162. return obj;
  163. }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement