Update http.js

This commit is contained in:
Namhyeon Go 2022-01-03 13:17:00 +09:00 committed by GitHub
parent ada2db6f77
commit 966f554d0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -203,21 +203,35 @@ var HTTPObject = function() {
} }
}; };
this.serializeURL = function(obj) { this.serializeURL = function(parametersObject) {
var str = []; var str = [];
for (var p in obj) for (var k in parametersObject) {
if (obj.hasOwnProperty(p)) { if (parametersObject.hasOwnProperty(k)) {
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p])); str.push(encodeURIComponent(k) + "=" + encodeURIComponent(parametersObject[k]));
}
} }
return str.join("&"); return str.join("&");
}; };
// Type 1: http://domain?a=1&b=2&c=3
// Type 2: http://domain/:a/:b/:c
this.serializeParameters = function(url) { this.serializeParameters = function(url) {
if (Object.keys(this.parameters).length > 0) { 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) { if (url.indexOf('?') > -1) {
return url + '&' + this.serializeURL(this.parameters); return url + '&' + this.serializeURL(parameters);
} else { } else {
return url + '?' + this.serializeURL(this.parameters); return url + '?' + this.serializeURL(parameters);
} }
} else { } else {
return url; return url;
@ -306,8 +320,8 @@ var HTTPObject = function() {
this.get = function(url, callback) { this.get = function(url, callback) {
try { try {
if (this.jqEnabled()) { if (this.jqEnabled()) {
return this.jqAjax({ return this.jqAjax({
type: "GET", type: "GET",
headers: this.headers, headers: this.headers,
url: this.serializeParameters(url), url: this.serializeParameters(url),
@ -315,13 +329,13 @@ var HTTPObject = function() {
contentType: this.contentType, contentType: this.contentType,
success: callback, success: callback,
async: this.isAsync, async: this.isAsync,
error: function(request, status, error) { 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 { } else {
return this.open("GET", url).send(callback); return this.open("GET", url).send(callback);
} }
} catch (e) { } catch (e) {
console.error("HTTPObject.get() -> ", e.message); console.error("HTTPObject.get() -> ", e.message);
} }
@ -339,8 +353,8 @@ var HTTPObject = function() {
success: callback, success: callback,
async: this.isAsync, async: this.isAsync,
error: function(request, status, error) { 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 { } else {
throw Error("PATCH does not supported on GUI mode"); throw Error("PATCH does not supported on GUI mode");