From 966f554d0fde4ce07278ed751ee98f1ff533fdf2 Mon Sep 17 00:00:00 2001 From: "Namhyeon, Go" Date: Mon, 3 Jan 2022 13:17:00 +0900 Subject: [PATCH] Update http.js --- lib/http.js | 48 +++++++++++++++++++++++++++++++----------------- 1 file changed, 31 insertions(+), 17 deletions(-) diff --git a/lib/http.js b/lib/http.js index 0d0e500..a8f4f96 100644 --- a/lib/http.js +++ b/lib/http.js @@ -203,21 +203,35 @@ var HTTPObject = function() { } }; - this.serializeURL = function(obj) { + this.serializeURL = function(parametersObject) { var str = []; - for (var p in obj) - if (obj.hasOwnProperty(p)) { - str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); + for (var k in parametersObject) { + if (parametersObject.hasOwnProperty(k)) { + str.push(encodeURIComponent(k) + "=" + encodeURIComponent(parametersObject[k])); + } } return str.join("&"); }; + // Type 1: http://domain?a=1&b=2&c=3 + // Type 2: http://domain/:a/:b/:c this.serializeParameters = function(url) { if (Object.keys(this.parameters).length > 0) { + // Type 2 + var parameters = {}; + for (var k in this.parameters) { + if (url.indexOf(':' + k) > -1) { + url = url.replace(':' + k, this.parameters[k]); + } else { + parameters[k] = this.parameters[k]; + } + } + + // Type 1 if (url.indexOf('?') > -1) { - return url + '&' + this.serializeURL(this.parameters); + return url + '&' + this.serializeURL(parameters); } else { - return url + '?' + this.serializeURL(this.parameters); + return url + '?' + this.serializeURL(parameters); } } else { return url; @@ -306,8 +320,8 @@ var HTTPObject = function() { this.get = function(url, callback) { try { - if (this.jqEnabled()) { - return this.jqAjax({ + if (this.jqEnabled()) { + return this.jqAjax({ type: "GET", headers: this.headers, url: this.serializeParameters(url), @@ -315,13 +329,13 @@ var HTTPObject = function() { contentType: this.contentType, success: callback, async: this.isAsync, - error: function(request, status, error) { - console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error); - } - }); - } else { - return this.open("GET", url).send(callback); - } + error: function(request, status, error) { + console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error); + } + }); + } else { + return this.open("GET", url).send(callback); + } } catch (e) { console.error("HTTPObject.get() -> ", e.message); } @@ -339,8 +353,8 @@ var HTTPObject = function() { success: callback, async: this.isAsync, error: function(request, status, error) { - console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error); - } + console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error); + } }); } else { throw Error("PATCH does not supported on GUI mode");