@easyquery/core
Version:
EasyQuery.JS core modules
41 lines • 1.51 kB
JavaScript
export function ajax(options) {
var XDomainRequest = window["XDomainRequest"];
var XHR = ("onload" in new XMLHttpRequest()) ? XMLHttpRequest : XDomainRequest; //IE support
var xhr = new XHR();
var type = options.type || 'POST';
xhr.open(type, options.url, true);
var contentType = options.contentType || "application/json";
xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
xhr.setRequestHeader("Content-Type", contentType);
if (options.beforeSend) {
options.beforeSend(xhr, options.data);
}
if (typeof options.data !== "string" && contentType.indexOf('application/json') == 0) {
options.data = JSON.stringify(options.data);
}
xhr.onreadystatechange = function () {
if (xhr.readyState != 4) {
return;
}
var status = parseInt(xhr.status);
if (status >= 300 || status < 200) {
if (options.error) {
options.error(xhr, status, xhr.statusText);
}
return;
}
if (options.success) {
var dataType = options.dataType || xhr.responseType;
if (dataType === "json") {
var response = JSON.parse(xhr.responseText);
options.success(response);
}
else {
options.success(xhr.responseText);
}
}
};
xhr.send(options.data);
return xhr;
}
//# sourceMappingURL=ajax.js.map