mirror of
https://github.com/gnh1201/welsonjs.git
synced 2025-06-18 02:59:04 +00:00
Update http.js
This commit is contained in:
parent
ada2db6f77
commit
966f554d0f
48
lib/http.js
48
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");
|
||||
|
|
Loading…
Reference in New Issue
Block a user