Files
NapCatQQ/.pnpm-store/v3/files/99/e43bd6a167be4538584c1a8a02c29894adbc458a5afeeb0f1edee0c18fd21bc961a851aaae1f931877f5d28726556f802a76e602949314bad1b65ef0eaf81b
TanTan e311ca3275
Build NapCat Artifacts / Windows Loader Test (push) Canceled after 0s
Build NapCat Artifacts / Type Check & Test (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Framework (push) Canceled after 0s
Build NapCat Artifacts / Build NapCat.Shell (push) Canceled after 0s
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

80 lines
2.2 KiB
Plaintext

"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = ajaxFile;
exports.loadFile = loadFile;
/**
* @file ajax获取文本数据
* @author mengke01(kekee000@gmail.com)
*/
/**
* ajax获取数据
*
* @param {Object} options 参数选项
* @param {string=} options.type 类型
* @param {string=} options.method method
* @param {Function=} options.onSuccess 成功回调
* @param {Function=} options.onError 失败回调
* @param {Object=} options.params 参数集合
*/
function ajaxFile(options) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
var status = xhr.status;
if (status >= 200 && status < 300 || status === 304) {
if (options.onSuccess) {
if (options.type === 'binary') {
var buffer = xhr.responseBlob || xhr.response;
options.onSuccess(buffer);
} else if (options.type === 'xml') {
options.onSuccess(xhr.responseXML);
} else if (options.type === 'json') {
options.onSuccess(JSON.parse(xhr.responseText));
} else {
options.onSuccess(xhr.responseText);
}
}
} else if (options.onError) {
options.onError(xhr, xhr.status);
}
}
};
var method = (options.method || 'GET').toUpperCase();
var params = null;
if (options.params) {
var str = [];
Object.keys(options.params).forEach(function (key) {
str.push(key + '=' + encodeURIComponent(options.params[key]));
});
str = str.join('&');
if (method === 'GET') {
options.url += (options.url.indexOf('?') === -1 ? '?' : '&') + str;
} else {
params = str;
}
}
xhr.open(method, options.url, true);
if (options.type === 'binary') {
xhr.responseType = 'arraybuffer';
}
xhr.send(params);
}
function loadFile(url) {
var type = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'binary';
return new Promise(function (resolve, reject) {
ajaxFile({
type: type,
url: url,
onSuccess: function onSuccess(buffer) {
resolve(buffer);
},
onError: function onError(e) {
reject(e);
}
});
});
}