Update http.js

This commit is contained in:
Namhyeon Go 2020-11-26 17:13:13 +09:00
parent 3ead445d70
commit c38349e71f

View File

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