|
|
@@ -20,146 +20,147 @@ function ajax(options) {
|
|
|
} else {
|
|
|
return parseResponse(xhr, options);
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
-ajax.defaults = {
|
|
|
- async: true,
|
|
|
- dataType: "json",
|
|
|
- timeout: 30,
|
|
|
- type: "GET",
|
|
|
-};
|
|
|
-// contentType
|
|
|
-// context
|
|
|
-// data
|
|
|
-// error
|
|
|
-// headers
|
|
|
-// progress
|
|
|
-// success
|
|
|
-// url
|
|
|
-
|
|
|
-var applyDefaults = function (options) {
|
|
|
- for (var key in ajax.defaults) {
|
|
|
- if (options[key] == null) {
|
|
|
- options[key] = ajax.defaults[key];
|
|
|
+ function applyDefaults(options) {
|
|
|
+ for (var key in ajax.defaults) {
|
|
|
+ if (options[key] == null) {
|
|
|
+ options[key] = ajax.defaults[key];
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var serializeData = function (options) {
|
|
|
- if (!options.data) {
|
|
|
- return;
|
|
|
- }
|
|
|
+ function serializeData(options) {
|
|
|
+ if (!options.data) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
|
|
|
- if (options.type === "GET") {
|
|
|
- options.url += "?" + serializeParams(options.data);
|
|
|
- options.data = null;
|
|
|
- } else {
|
|
|
- options.data = serializeParams(options.data);
|
|
|
+ if (options.type === "GET") {
|
|
|
+ options.url += "?" + serializeParams(options.data);
|
|
|
+ options.data = null;
|
|
|
+ } else {
|
|
|
+ options.data = serializeParams(options.data);
|
|
|
+ }
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var serializeParams = (params) =>
|
|
|
- Object.entries(params)
|
|
|
- .map(
|
|
|
- ([key, value]) =>
|
|
|
- `${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
|
|
|
- )
|
|
|
- .join("&");
|
|
|
-
|
|
|
-var applyCallbacks = function (xhr, options) {
|
|
|
- if (!options.async) {
|
|
|
- return;
|
|
|
+ function serializeParams(params) {
|
|
|
+ return Object.entries(params)
|
|
|
+ .map(
|
|
|
+ ([key, value]) =>
|
|
|
+ `${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
|
|
|
+ )
|
|
|
+ .join("&");
|
|
|
}
|
|
|
|
|
|
- xhr.timer = setTimeout(
|
|
|
- onTimeout.bind(undefined, xhr, options),
|
|
|
- options.timeout * 1000,
|
|
|
- );
|
|
|
- if (options.progress) {
|
|
|
- xhr.onprogress = options.progress;
|
|
|
- }
|
|
|
- xhr.onreadystatechange = function () {
|
|
|
- if (xhr.readyState === 4) {
|
|
|
- clearTimeout(xhr.timer);
|
|
|
- onComplete(xhr, options);
|
|
|
+ function applyCallbacks(xhr, options) {
|
|
|
+ if (!options.async) {
|
|
|
+ return;
|
|
|
}
|
|
|
- };
|
|
|
-};
|
|
|
|
|
|
-var applyHeaders = function (xhr, options) {
|
|
|
- if (!options.headers) {
|
|
|
- options.headers = {};
|
|
|
+ xhr.timer = setTimeout(
|
|
|
+ onTimeout.bind(undefined, xhr, options),
|
|
|
+ options.timeout * 1000,
|
|
|
+ );
|
|
|
+ if (options.progress) {
|
|
|
+ xhr.onprogress = options.progress;
|
|
|
+ }
|
|
|
+ xhr.onreadystatechange = function () {
|
|
|
+ if (xhr.readyState === 4) {
|
|
|
+ clearTimeout(xhr.timer);
|
|
|
+ onComplete(xhr, options);
|
|
|
+ }
|
|
|
+ };
|
|
|
}
|
|
|
|
|
|
- if (options.contentType) {
|
|
|
- options.headers["Content-Type"] = options.contentType;
|
|
|
- }
|
|
|
+ function applyHeaders(xhr, options) {
|
|
|
+ if (!options.headers) {
|
|
|
+ options.headers = {};
|
|
|
+ }
|
|
|
|
|
|
- if (
|
|
|
- !options.headers["Content-Type"] &&
|
|
|
- options.data &&
|
|
|
- options.type !== "GET"
|
|
|
- ) {
|
|
|
- options.headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
|
- }
|
|
|
+ if (options.contentType) {
|
|
|
+ options.headers["Content-Type"] = options.contentType;
|
|
|
+ }
|
|
|
|
|
|
- if (options.dataType) {
|
|
|
- options.headers["Accept"] =
|
|
|
- MIME_TYPES[options.dataType] || options.dataType;
|
|
|
- }
|
|
|
+ if (
|
|
|
+ !options.headers["Content-Type"] &&
|
|
|
+ options.data &&
|
|
|
+ options.type !== "GET"
|
|
|
+ ) {
|
|
|
+ options.headers["Content-Type"] = "application/x-www-form-urlencoded";
|
|
|
+ }
|
|
|
+
|
|
|
+ if (options.dataType) {
|
|
|
+ options.headers["Accept"] =
|
|
|
+ MIME_TYPES[options.dataType] || options.dataType;
|
|
|
+ }
|
|
|
|
|
|
- for (var key in options.headers) {
|
|
|
- var value = options.headers[key];
|
|
|
- xhr.setRequestHeader(key, value);
|
|
|
+ for (var key in options.headers) {
|
|
|
+ var value = options.headers[key];
|
|
|
+ xhr.setRequestHeader(key, value);
|
|
|
+ }
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var onComplete = function (xhr, options) {
|
|
|
- if (200 <= xhr.status && xhr.status < 300) {
|
|
|
- let response;
|
|
|
- if ((response = parseResponse(xhr, options)) != null) {
|
|
|
- onSuccess(response, xhr, options);
|
|
|
+ function onComplete(xhr, options) {
|
|
|
+ if (200 <= xhr.status && xhr.status < 300) {
|
|
|
+ let response;
|
|
|
+ if ((response = parseResponse(xhr, options)) != null) {
|
|
|
+ onSuccess(response, xhr, options);
|
|
|
+ } else {
|
|
|
+ onError("invalid", xhr, options);
|
|
|
+ }
|
|
|
} else {
|
|
|
- onError("invalid", xhr, options);
|
|
|
+ onError("error", xhr, options);
|
|
|
}
|
|
|
- } else {
|
|
|
- onError("error", xhr, options);
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var onSuccess = function (response, xhr, options) {
|
|
|
- if (options.success != null) {
|
|
|
- options.success.call(options.context, response, xhr, options);
|
|
|
+ function onSuccess(response, xhr, options) {
|
|
|
+ if (options.success != null) {
|
|
|
+ options.success.call(options.context, response, xhr, options);
|
|
|
+ }
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var onError = function (type, xhr, options) {
|
|
|
- if (options.error != null) {
|
|
|
- options.error.call(options.context, type, xhr, options);
|
|
|
+ function onError(type, xhr, options) {
|
|
|
+ if (options.error != null) {
|
|
|
+ options.error.call(options.context, type, xhr, options);
|
|
|
+ }
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var onTimeout = function (xhr, options) {
|
|
|
- xhr.abort();
|
|
|
- onError("timeout", xhr, options);
|
|
|
-};
|
|
|
+ function onTimeout(xhr, options) {
|
|
|
+ xhr.abort();
|
|
|
+ onError("timeout", xhr, options);
|
|
|
+ }
|
|
|
|
|
|
-var abort = function (xhr) {
|
|
|
- clearTimeout(xhr.timer);
|
|
|
- xhr.onreadystatechange = null;
|
|
|
- xhr.abort();
|
|
|
-};
|
|
|
+ function abort(xhr) {
|
|
|
+ clearTimeout(xhr.timer);
|
|
|
+ xhr.onreadystatechange = null;
|
|
|
+ xhr.abort();
|
|
|
+ }
|
|
|
|
|
|
-var parseResponse = function (xhr, options) {
|
|
|
- if (options.dataType === "json") {
|
|
|
- return parseJSON(xhr.responseText);
|
|
|
- } else {
|
|
|
- return xhr.responseText;
|
|
|
+ function parseResponse(xhr, options) {
|
|
|
+ if (options.dataType === "json") {
|
|
|
+ return parseJSON(xhr.responseText);
|
|
|
+ } else {
|
|
|
+ return xhr.responseText;
|
|
|
+ }
|
|
|
}
|
|
|
-};
|
|
|
|
|
|
-var parseJSON = function (json) {
|
|
|
- try {
|
|
|
- return JSON.parse(json);
|
|
|
- } catch (error) {}
|
|
|
+ function parseJSON(json) {
|
|
|
+ try {
|
|
|
+ return JSON.parse(json);
|
|
|
+ } catch (error) {}
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+ajax.defaults = {
|
|
|
+ async: true,
|
|
|
+ dataType: "json",
|
|
|
+ timeout: 30,
|
|
|
+ type: "GET",
|
|
|
+ // contentType
|
|
|
+ // context
|
|
|
+ // data
|
|
|
+ // error
|
|
|
+ // headers
|
|
|
+ // progress
|
|
|
+ // success
|
|
|
+ // url
|
|
|
};
|