Files
NapCatQQ/.pnpm-store/v3/files/ff/f717fd784ec56fa26d8aea26405c3cfb26dccb97ab7e31de2b9dc18bcb816e301d06f4016634801bb7368c5e6d6d31994d8dec7303e54832cec641dce39f60
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

86 lines
2.3 KiB
Plaintext

/**
* @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 参数集合
*/
export default function ajaxFile(options) {
const xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
const status = xhr.status;
if (status >= 200 && status < 300 || status === 304) {
if (options.onSuccess) {
if (options.type === 'binary') {
const 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);
}
}
};
const method = (options.method || 'GET').toUpperCase();
let params = null;
if (options.params) {
let str = [];
Object.keys(options.params).forEach(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);
}
export function loadFile(url, type = 'binary') {
return new Promise((resolve, reject) => {
ajaxFile({
type,
url,
onSuccess(buffer) {
resolve(buffer);
},
onError(e) {
reject(e);
}
});
});
}