Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- | Vanilla JavaScript AJAX Requests
- | = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
- | @author: Tommy Vercety
- | @version: v0.01 alpha
- | @license: MIT
- |
- | Works on almost all browsers. Implemented getJSON and postJSON methods. For
- | everything else use make();
- |
- | Example:
- | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- |
- | var ajax = new MY.VanillaAjax();
- |
- | ajax.getJSON("http://httpbin.org/get?hello=world", function(data) {
- | console.log(data);
- | });
- |
- | var data = JSON.stringify({
- | name: "tommy",
- | pass: "qwerty"
- | });
- |
- | ajax.postJSON("http://httpbin.org/post", data, function(data) {
- | console.log(data);
- | });
- |
- | ISSUES:
- | - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- |
- | - some problems sending POST data!
- |
- */
- var MY = MY || {}; // namespace
- MY.VanillaAjax = (function() {
- /**
- * Constructor.
- *
- * Get the right httpRequest based on your browser
- * @return object httpRequest instance
- */
- function vanillaAjax() {
- vanillaAjax.getHttpRequest = (function () {
- var xmlHttpFactories = [
- function() { return new XMLHttpRequest(); },
- function() { return new ActiveXObject("Msxml3.XMLHTTP"); },
- function() { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); },
- function() { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); },
- function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
- function() { return new ActiveXObject("Microsoft.XMLHTTP"); }
- ];
- return function() {
- var xmlFactory, i, len;
- for(i = 0, len = xmlHttpFactories.length; i < len; i++) {
- xmlFactory = xmlHttpFactories[i];
- try {
- return xmlFactory();
- } catch (error) {
- console.log(error);
- }
- }
- return null;
- };
- })();
- }
- /**
- * Make ajax call
- *
- * @param object options
- * @return mixed ajax call data
- */
- vanillaAjax.prototype.make = function(options) {
- var httpRequest, requestUrl, type, success, error, contentType, accept, data;
- httpRequest = vanillaAjax.getHttpRequest();
- options = options || {};
- requestUrl = options.url;
- type = options.type || 'GET';
- success = options.success || function () {};
- error = options.error || function () {};
- contentType = options.contentType || '';
- accept = options.accept || '';
- data = options.data || null;
- httpRequest.onreadystatechange = function() {
- if (httpRequest.readyState === 4) {
- switch(Math.floor(httpRequest.status / 100)) {
- case 2:
- success(httpRequest.responseText);
- break;
- default:
- error(httpRequest.responseText);
- break;
- }
- }
- };
- httpRequest.open(type, requestUrl, true);
- httpRequest.setRequestHeader('Content-Type', contentType);
- httpRequest.setRequestHeader('Accept', accept);
- return httpRequest.send(data);
- };
- /**
- * Direct JSON GET Requst
- *
- * @param string url
- * @param fucntion success
- * @param function error
- * @return mixed ajax call data
- */
- vanillaAjax.prototype.getJSON = function(url, success, error) {
- var options = {
- url: url,
- type: 'GET',
- contentType: 'application/json; charset=utf-8',
- accept: 'application/json',
- success: function(data) {
- if(!success) {return;}
- if(data) {
- return success(JSON.parse(data));
- } else {
- return success();
- }
- },
- error: function(err) {
- if(!error) {return;}
- if(err) {
- return error(JSON.parse(err));
- } else {
- return error();
- }
- }
- };
- return vanillaAjax.prototype.make(options);
- };
- /**
- * Direct JSON POST Requst
- *
- * @param string url
- * @param object data data to be sent
- * @param fucntion success
- * @param function error
- * @return mixed ajax call data
- */
- vanillaAjax.prototype.postJSON = function(url, data, success, error) {
- var options = {
- url: url,
- type: 'POST',
- contentType: 'application/json; charset=utf-8',
- accept: 'application/json',
- data: data,
- success: function(data) {
- if(!success) {return;}
- if(data) {
- return success(JSON.parse(data));
- } else {
- return success();
- }
- },
- error: function(err) {
- if(!error) {return;}
- if(err) {
- return error(JSON.parse(err));
- } else {
- return error();
- }
- }
- };
- return vanillaAjax.prototype.make(options);
- };
- return vanillaAjax;
- }());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement