Advertisement
_cronos2

JSPlus__AJAX

Sep 5th, 2011
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. JSPlus.ajax = function(obj){
  2.     var xhr = (function(){
  3.         var xhr = false;
  4.         if (window.XMLHttpRequest) { // Chrome, FF, Safari,...
  5.             (xhr = new XMLHttpRequest()).overrideMimeType && xhr.overrideMimeType('text/xml');
  6.         } else if (window.ActiveXObject) { // IE
  7.             try {
  8.                 xhr = new ActiveXObject("Msxml2.XMLHTTP");
  9.             } catch (e) {
  10.                 try {
  11.                     xhr = new ActiveXObject("Microsoft.XMLHTTP");
  12.                 } catch (e) {
  13.                     console.log('Tu navegador es muy antiguo, no soporta AJAX');
  14.                     throw e;
  15.                 }
  16.             }
  17.         }
  18.         return xhr;
  19.     })();
  20.    
  21.     JSPlus.calcar(obj, JSPlus.ajax.porDefecto);
  22.    
  23.     xhr.open(obj.metodo, obj.url, obj.async);
  24.     for(var i in obj.headers){
  25.         obj.headers.hasOwnProperty(i) && xhr.setRequestHeader(i, obj.headers[i]);
  26.     }
  27.    
  28.     xhr.onreadystatechange = function(){
  29.         console.log(obj);
  30.         xhr.readyState == 4 && xhr.status == 200 && obj.ok(xhr.responseText);
  31.     };
  32.    
  33.     xhr.send(obj.info);
  34.    
  35. };
  36.  
  37. JSPlus.ajax.porDefecto = {
  38.     'url' : 'index.php',
  39.     'metodo' : 'GET',
  40.     'info' : '',
  41.     'ok' : function(){},
  42.     'async' : true,
  43.     'headers' : {},
  44.     'contexto' : null
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement