Advertisement
danilomalzao

AJAX request

Jul 20th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.          * Pega o objeto de request ajax.
  3.          *
  4.          * Função compativel com todos os browsers.
  5.          * @returns {XMLHttpRequest} Objeto de se fazer requisições ajax.
  6.          */
  7.         function _getXHR() {
  8.             if (window.XMLHttpRequest) {
  9.                 // Chrome, Firefox, IE7+, Opera, Safari
  10.                 return new XMLHttpRequest();
  11.             }
  12.             // IE6
  13.             try {
  14.                 // The latest stable version. It has the best security, performance,
  15.                 // reliability, and W3C conformance. Ships with Vista, and available
  16.                 // with other OS's via downloads and updates.
  17.                 return new ActiveXObject('MSXML2.XMLHTTP.6.0');
  18.             } catch (e) {
  19.                 try {
  20.                     // The fallback.
  21.                     return new ActiveXObject('MSXML2.XMLHTTP.3.0');
  22.                 } catch (e) {
  23.                     alert('This browser is not AJAX enabled.');
  24.                     return null;
  25.                 }
  26.             }
  27.         }
  28.  
  29.  
  30.         /**
  31.          * Faz uma requisição AJAX no mesmo dominio.
  32.          *
  33.          * PS: para carregar dados de outros dominios, deve-se usar a requisição 'CORS'.
  34.          * @param url URL que será carregada, incluindo o 'http://'.
  35.          * @param success Função a ser executada em caso de sucesso.
  36.          * @param failed Função a ser executado em caso de falha.
  37.          * @param forceSynchronous True para fazer a requisição sincrona (deprecated), False caso contrário.
  38.          * @param scope Escopo das funções success e failed.
  39.          */
  40.         function get(url, success, failed, forceSynchronous, scope) {
  41.             var xmlHttp = _getXHR();
  42.             if (forceSynchronous == undefined || !forceSynchronous) {
  43.                 xmlHttp.onreadystatechange = function() {
  44.                     if (xmlHttp.readyState == 4)
  45.                         if (xmlHttp.status == 200 && success)
  46.                             success.call(scope, xmlHttp);
  47.                         else
  48.                             failed.call(scope, xmlHttp);
  49.                 }
  50.                 xmlHttp.open("GET", url, true); // true for asynchronous
  51.                 xmlHttp.send(null);
  52.             } else {
  53.                 xmlHttp.open("GET", url, false); // true for asynchronous
  54.                 xmlHttp.send(null);
  55.                 if (xmlHttp.status === 200)
  56.                     success.call(scope, xmlHttp);
  57.                 else
  58.                     failed.call(scope, xmlHttp);
  59.             }
  60.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement