|
|
|
@ -20,32 +20,16 @@ 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) {
|
|
|
|
|
function applyDefaults(options) {
|
|
|
|
|
for (var key in ajax.defaults) {
|
|
|
|
|
if (options[key] == null) {
|
|
|
|
|
options[key] = ajax.defaults[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var serializeData = function (options) {
|
|
|
|
|
function serializeData(options) {
|
|
|
|
|
if (!options.data) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -56,17 +40,18 @@ var serializeData = function (options) {
|
|
|
|
|
} else {
|
|
|
|
|
options.data = serializeParams(options.data);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var serializeParams = (params) =>
|
|
|
|
|
Object.entries(params)
|
|
|
|
|
function serializeParams(params) {
|
|
|
|
|
return Object.entries(params)
|
|
|
|
|
.map(
|
|
|
|
|
([key, value]) =>
|
|
|
|
|
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
|
|
|
|
|
)
|
|
|
|
|
.join("&");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var applyCallbacks = function (xhr, options) {
|
|
|
|
|
function applyCallbacks(xhr, options) {
|
|
|
|
|
if (!options.async) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
@ -84,9 +69,9 @@ var applyCallbacks = function (xhr, options) {
|
|
|
|
|
onComplete(xhr, options);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var applyHeaders = function (xhr, options) {
|
|
|
|
|
function applyHeaders(xhr, options) {
|
|
|
|
|
if (!options.headers) {
|
|
|
|
|
options.headers = {};
|
|
|
|
|
}
|
|
|
|
@ -112,9 +97,9 @@ var applyHeaders = function (xhr, options) {
|
|
|
|
|
var value = options.headers[key];
|
|
|
|
|
xhr.setRequestHeader(key, value);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var onComplete = function (xhr, options) {
|
|
|
|
|
function onComplete(xhr, options) {
|
|
|
|
|
if (200 <= xhr.status && xhr.status < 300) {
|
|
|
|
|
let response;
|
|
|
|
|
if ((response = parseResponse(xhr, options)) != null) {
|
|
|
|
@ -125,41 +110,57 @@ var onComplete = function (xhr, options) {
|
|
|
|
|
} else {
|
|
|
|
|
onError("error", xhr, options);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var onSuccess = function (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) {
|
|
|
|
|
function onError(type, xhr, options) {
|
|
|
|
|
if (options.error != null) {
|
|
|
|
|
options.error.call(options.context, type, xhr, options);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var onTimeout = function (xhr, options) {
|
|
|
|
|
function onTimeout(xhr, options) {
|
|
|
|
|
xhr.abort();
|
|
|
|
|
onError("timeout", xhr, options);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var abort = function (xhr) {
|
|
|
|
|
function abort(xhr) {
|
|
|
|
|
clearTimeout(xhr.timer);
|
|
|
|
|
xhr.onreadystatechange = null;
|
|
|
|
|
xhr.abort();
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parseResponse = function (xhr, options) {
|
|
|
|
|
function parseResponse(xhr, options) {
|
|
|
|
|
if (options.dataType === "json") {
|
|
|
|
|
return parseJSON(xhr.responseText);
|
|
|
|
|
} else {
|
|
|
|
|
return xhr.responseText;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var parseJSON = function (json) {
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|