diff --git a/lib/http.js b/lib/http.js index ff8b788..9690385 100644 --- a/lib/http.js +++ b/lib/http.js @@ -9,12 +9,13 @@ var HTTPObject = function() { this.contentType = "application/x-www-form-urlencoded"; this.requestBody = ""; this.responseBody = null; - this.method = ""; + this.method = "GET"; this.headers = {}; this.parameters = {}; - this.dataType = ""; + this.dataType = null; this.userAgent = "WelsonJS/0.1.4-dev (https://github.com/gnh1201/welsonjs)"; - + this.isAsync = false; + this.create = function() { this.interface = CreateObject([ "Microsoft.XMLHTTP", @@ -40,7 +41,15 @@ var HTTPObject = function() { } }); return this; - } + }; + + this.jqEnabled = function() { + return (typeof(window) !== "undefined" && typeof(window.jQuery) !== "undefined"); + }; + + this.jqAjax = function(options) { + return (this.jqEnabled() ? window.jQuery.ajax(options) : null); + }; this.isJSONRequest = function() { return (this.contentType === "application/json"); @@ -169,6 +178,11 @@ var HTTPObject = function() { return this; }; + this.setUseAsync = function(flag) { + this.isAsync = flag; + return this; + } + this.setUserAgent = function(agent) { this.userAgent = agent; this.setHeader("User-Agent", this.userAgent); @@ -205,17 +219,17 @@ var HTTPObject = function() { } }; - this.open = function(method, url, isAsync) { + this.open = function(method, url) { this.setMethod(method.toUpperCase()); try { switch (this.method) { case "POST": - this.interface.open(method, this.serializeParameters(url), isAsync); + this.interface.open(method, this.serializeParameters(url), this.isAsync); break; case "GET": - this.interface.open(method, this.serializeParameters(url), isAsync); + this.interface.open(method, this.serializeParameters(url), this.isAsync); break; case "PATCH": @@ -265,35 +279,36 @@ var HTTPObject = function() { }; this.post = function(url, callback) { - return this.open("POST", url, false).send(callback); + try { + return this.open("POST", url).send(callback); + } catch (e) { + console.error("HTTPObject.post() -> ", e.message); + } }; this.get = function(url, callback) { - return this.open("GET", url, false).send(callback); + try { + return this.open("GET", url).send(callback); + } catch (e) { + console.error("HTTPObject.get() -> ", e.message); + } }; this.patch = function(url, callback) { try { - var options = { - type: "PATCH", - headers: this.headers, - url: url, - data: this.requestBody, - contentType: this.contentType, - success: callback - }; - - if (typeof(window) !== "undefined") { - if (typeof(window.jQuery) !== "undefined") { - window.jQuery.ajax(options); - } else { - console.error("PATCH is required jQuery library"); - } + if (this.jqEnabled()) { + return this.jqAjax({ + type: "PATCH", + headers: this.headers, + url: url, + data: this.requestBody, + contentType: this.contentType, + success: callback, + async: this.isAsync + }); } else { - console.error("PATCH dose not supported on console mode"); + throw Error("PATCH does not supported on GUI mode"); } - - return this; } catch (e) { console.error("HTTPObject.patch() -> ", e.message); }