From d64fac7c2f27abcb5a39a94608c047b49843b0eb Mon Sep 17 00:00:00 2001
From: Commander-lol <ljcapitanio@gmail.com>
Date: Sat, 1 Nov 2014 19:05:25 +0000
Subject: [PATCH] Added AJAX

Added ajax support as a utility function. All options are passed in a
single object, as well as an optional success and error function
---
 js/tinyDOM.js | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/js/tinyDOM.js b/js/tinyDOM.js
index b9c571a..623809c 100644
--- a/js/tinyDOM.js
+++ b/js/tinyDOM.js
@@ -93,6 +93,57 @@
 		}
 	}
 
+	tinyDOM.ajax = function(options){
+		var req = new XMLHttpRequest();
+		var _this = this;
+
+		var params = {
+			method: 'GET',
+			url: '',
+			async: true,
+			user: null,
+			password: null,
+			responseType: 'text',
+			data: null,
+			headers: [],
+			success: null,
+			error: null
+		}
+
+		this.merge(params, options);
+
+		req.responseType = params.responseType;
+
+		req.onreadystatechange = function(){
+			if(req.readyState > 1){
+				if(req.status === 200){
+					if (req.readyState === 4 && _this.exists(params.success)){
+						params.success(req.response, req);
+					}
+				} else {
+					req.abort();
+					if(_this.exists(params.error)){
+						params.error({status: req.status, message: req.statusText}, req);
+					}
+				}
+			}
+		}
+
+		req.open(
+			params.method,
+			params.url,
+			params.async,
+			params.user,
+			params.password
+		);
+
+		for(var i = 0; i < params.headers.length; i++){
+			req.setRequestHeader(params.headers[i].header, params.headers[i].value);
+		}
+
+		req.send(params.data);
+	};
+
 	if(!window.μ){
 		window.μ = tinyDOM;
 	}
-- 
GitLab