Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Pega o objeto de request ajax.
- *
- * Função compativel com todos os browsers.
- * @returns {XMLHttpRequest} Objeto de se fazer requisições ajax.
- */
- function _getXHR() {
- if (window.XMLHttpRequest) {
- // Chrome, Firefox, IE7+, Opera, Safari
- return new XMLHttpRequest();
- }
- // IE6
- try {
- // The latest stable version. It has the best security, performance,
- // reliability, and W3C conformance. Ships with Vista, and available
- // with other OS's via downloads and updates.
- return new ActiveXObject('MSXML2.XMLHTTP.6.0');
- } catch (e) {
- try {
- // The fallback.
- return new ActiveXObject('MSXML2.XMLHTTP.3.0');
- } catch (e) {
- alert('This browser is not AJAX enabled.');
- return null;
- }
- }
- }
- /**
- * Faz uma requisição AJAX no mesmo dominio.
- *
- * PS: para carregar dados de outros dominios, deve-se usar a requisição 'CORS'.
- * @param url URL que será carregada, incluindo o 'http://'.
- * @param success Função a ser executada em caso de sucesso.
- * @param failed Função a ser executado em caso de falha.
- * @param forceSynchronous True para fazer a requisição sincrona (deprecated), False caso contrário.
- * @param scope Escopo das funções success e failed.
- */
- function get(url, success, failed, forceSynchronous, scope) {
- var xmlHttp = _getXHR();
- if (forceSynchronous == undefined || !forceSynchronous) {
- xmlHttp.onreadystatechange = function() {
- if (xmlHttp.readyState == 4)
- if (xmlHttp.status == 200 && success)
- success.call(scope, xmlHttp);
- else
- failed.call(scope, xmlHttp);
- }
- xmlHttp.open("GET", url, true); // true for asynchronous
- xmlHttp.send(null);
- } else {
- xmlHttp.open("GET", url, false); // true for asynchronous
- xmlHttp.send(null);
- if (xmlHttp.status === 200)
- success.call(scope, xmlHttp);
- else
- failed.call(scope, xmlHttp);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement