使用xhr获取数据时加入状态码判断

This commit is contained in:
nonameShijian 2024-02-08 00:49:50 +08:00
parent 50ce47eaac
commit 355f0cc5fd
1 changed files with 31 additions and 2 deletions

View File

@ -263,6 +263,11 @@ export class LibInit extends Uninstantable {
const xmlHttpRequest = new XMLHttpRequest(); const xmlHttpRequest = new XMLHttpRequest();
let data; let data;
xmlHttpRequest.addEventListener("load", () => { xmlHttpRequest.addEventListener("load", () => {
if (![0, 200].includes(xmlHttpRequest.status)) {
// @ts-ignore
if (typeof onError == 'function') onError(new Error(oReq.statusText || oReq.status));
return;
}
data = xmlHttpRequest.responseText; data = xmlHttpRequest.responseText;
if (!data) { if (!data) {
if (typeof onError == 'function') onError(new Error(`${scriptSource}加载失败!`)); if (typeof onError == 'function') onError(new Error(`${scriptSource}加载失败!`));
@ -303,7 +308,14 @@ export class LibInit extends Uninstantable {
sScriptURL = url + str; sScriptURL = url + str;
} }
const oReq = new XMLHttpRequest(); const oReq = new XMLHttpRequest();
if (typeof onload == 'function') oReq.addEventListener("load", onload); if (typeof onload == 'function') oReq.addEventListener("load", result => {
if (![0, 200].includes(oReq.status)) {
// @ts-ignore
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
return;
}
onload(result);
});
if (typeof onerror == 'function') oReq.addEventListener("error", onerror); if (typeof onerror == 'function') oReq.addEventListener("error", onerror);
oReq.open("GET", sScriptURL); oReq.open("GET", sScriptURL);
oReq.send(); oReq.send();
@ -330,7 +342,14 @@ export class LibInit extends Uninstantable {
sScriptURL = url + str; sScriptURL = url + str;
} }
const oReq = new XMLHttpRequest(); const oReq = new XMLHttpRequest();
if (typeof onload == 'function') oReq.addEventListener("load", onload); if (typeof onload == 'function') oReq.addEventListener("load", result => {
if (![0, 200].includes(oReq.status)) {
// @ts-ignore
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
return;
}
onload(result);
});
if (typeof onerror == 'function') oReq.addEventListener("error", onerror); if (typeof onerror == 'function') oReq.addEventListener("error", onerror);
oReq.open("GET", sScriptURL, false); oReq.open("GET", sScriptURL, false);
oReq.send(); oReq.send();
@ -340,6 +359,11 @@ export class LibInit extends Uninstantable {
static json(url, onload, onerror) { static json(url, onload, onerror) {
const oReq = new XMLHttpRequest(); const oReq = new XMLHttpRequest();
if (typeof onload == 'function') oReq.addEventListener("load", () => { if (typeof onload == 'function') oReq.addEventListener("load", () => {
if (![0, 200].includes(oReq.status)) {
// @ts-ignore
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
return;
}
let result; let result;
try { try {
result = JSON.parse(oReq.responseText); result = JSON.parse(oReq.responseText);
@ -368,6 +392,11 @@ export class LibInit extends Uninstantable {
} }
const oReq = new XMLHttpRequest(); const oReq = new XMLHttpRequest();
if (typeof onload == 'function') oReq.addEventListener("load", () => { if (typeof onload == 'function') oReq.addEventListener("load", () => {
if (![0, 200].includes(oReq.status)) {
// @ts-ignore
if (typeof onerror == 'function') onerror(new Error(oReq.statusText || oReq.status));
return;
}
let result; let result;
try { try {
result = JSON.parse(oReq.responseText); result = JSON.parse(oReq.responseText);