Spaces:
Running
Running
github-actions[bot] commited on
Commit ·
7e18e50
1
Parent(s): 7072a89
deploy: b58e3a0 — 更新 api.js
Browse files- frontend/src/api.js +11 -14
frontend/src/api.js
CHANGED
|
@@ -1,22 +1,19 @@
|
|
| 1 |
const BASE = "/api";
|
| 2 |
|
| 3 |
async function request(method, path, body) {
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
} catch (networkErr) {
|
| 12 |
-
throw new Error("Network error. Check your connection and try again.");
|
| 13 |
-
}
|
| 14 |
|
| 15 |
-
|
| 16 |
try {
|
| 17 |
-
data =
|
| 18 |
-
} catch (
|
| 19 |
-
throw new Error("
|
| 20 |
}
|
| 21 |
|
| 22 |
if (!data.success) throw new Error(data.error || "Request failed");
|
|
|
|
| 1 |
const BASE = "/api";
|
| 2 |
|
| 3 |
async function request(method, path, body) {
|
| 4 |
+
const res = await fetch(BASE + path, {
|
| 5 |
+
method: method,
|
| 6 |
+
headers: { "Content-Type": "application/json" },
|
| 7 |
+
body: body ? JSON.stringify(body) : undefined,
|
| 8 |
+
});
|
| 9 |
+
|
| 10 |
+
const text = await res.text();
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
+
let data;
|
| 13 |
try {
|
| 14 |
+
data = JSON.parse(text);
|
| 15 |
+
} catch (e) {
|
| 16 |
+
throw new Error("HTTP " + res.status + " -- server returned: " + text.slice(0, 200));
|
| 17 |
}
|
| 18 |
|
| 19 |
if (!data.success) throw new Error(data.error || "Request failed");
|