Update http.js

This commit is contained in:
Namhyeon Go 2022-06-09 11:54:32 +09:00 committed by GitHub
parent 31d4c26ac4
commit d25a12bd8b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -94,8 +94,17 @@ var HTTPObject = function(engine) {
return (typeof(window) !== "undefined" && typeof(window.jQuery) !== "undefined");
};
this.jqAjax = function(options) {
return (this.jqEnabled() ? window.jQuery.ajax(options) : null);
this.jqAjax = function(url, callback, onError) {
return window.jQuery.ajax({
type: this.method,
headers: this.headers,
url: this.serializeParameters(url),
data: this.requestBody,
contentType: this.contentType,
success: callback,
async: this.isAsync,
error: onError // (request, status, error)
});
};
this.isJSONRequest = function() {
@ -535,57 +544,42 @@ var HTTPObject = function(engine) {
return this;
};
this.post = function(url, callback) {
this.post = function(url, callback, onError) {
try {
return this.open("POST", url).send(callback);
if (this.jqEnabled()) {
this.setMethod("POST");
this.jqAjax(url, callback, onError);
} else {
return this.open("POST", url).send(callback);
}
} catch (e) {
console.error("HTTPObject.post() ->", e.message);
if (typeof onError === "function") onError(this, null, e);
}
};
this.get = function(url, callback) {
this.get = function(url, callback, onError) {
try {
if (this.jqEnabled()) {
return this.jqAjax({
type: "GET",
headers: this.headers,
url: this.serializeParameters(url),
data: this.requestBody,
contentType: this.contentType,
success: callback,
async: this.isAsync,
error: function(request, status, error) {
console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error);
}
});
this.setMethod("GET");
this.jqAjax(url, callback, onError);
} else {
return this.open("GET", url).send(callback);
}
} catch (e) {
console.error("HTTPObject.get() ->", e.message);
if (typeof onError === "function") onError(this, null, e);
}
};
this.patch = function(url, callback) {
this.patch = function(url, callback, onError) {
try {
if (this.jqEnabled()) {
return this.jqAjax({
type: "PATCH",
headers: this.headers,
url: this.serializeParameters(url),
data: this.requestBody,
contentType: this.contentType,
success: callback,
async: this.isAsync,
error: function(request, status, error) {
console.error("code: ", request.status, ", message: " + request.responseText + ", error: " + error);
}
});
this.setMethod("PATCH");
this.jqAjax(url, callback, onError);
} else {
throw Error("PATCH does not supported on GUI mode");
return this.open("PATCH", url).send(callback);
}
} catch (e) {
console.error("HTTPObject.patch() ->", e.message);
if (typeof onError === "function") onError(this, null, e);
}
};