Corin1998 commited on
Commit
1694978
·
verified ·
1 Parent(s): 17cc500

Update static/app.js

Browse files
Files changed (1) hide show
  1. static/app.js +21 -4
static/app.js CHANGED
@@ -7,9 +7,27 @@ async function postJSON(url, body){
7
  headers: {"Content-Type": "application/json"},
8
  body: JSON.stringify(body)
9
  });
10
- // 200以外でも本文を拾って表示したいのでここでは throw しない
11
  let data = null;
12
- try { data = await res.json(); } catch(_) { data = {ok:false, error:`${res.status} ${res.statusText}`}; }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
  return { status: res.status, data };
14
  }
15
 
@@ -32,8 +50,7 @@ document.getElementById("btnIngestUpload").onclick = async () => {
32
  try{
33
  const fd = new FormData();
34
  for(const f of files){ fd.append("files", f, f.name); }
35
- const res = await fetch("/ingest/upload", { method: "POST", body: fd });
36
- const data = await res.json();
37
  if(data.ok){ out.textContent = `保存:${data.saved?.length||0} / チャンク:${data.ingested_chunks}`; }
38
  else { out.textContent = "エラー: " + (data.error || "unknown"); }
39
  log("[UPLOAD] " + JSON.stringify(data));
 
7
  headers: {"Content-Type": "application/json"},
8
  body: JSON.stringify(body)
9
  });
 
10
  let data = null;
11
+ const ct = (res.headers.get("content-type") || "").toLowerCase();
12
+ if (ct.includes("application/json")) {
13
+ try { data = await res.json(); } catch(e){ data = {ok:false, error:`Invalid JSON: ${e.message}`}; }
14
+ } else {
15
+ const text = await res.text();
16
+ data = {ok:false, error:`HTTP ${res.status} ${res.statusText}: ${text.slice(0,500)}`};
17
+ }
18
+ return { status: res.status, data };
19
+ }
20
+
21
+ async function postForm(url, formData){
22
+ const res = await fetch(url, { method: "POST", body: formData });
23
+ let data = null;
24
+ const ct = (res.headers.get("content-type") || "").toLowerCase();
25
+ if (ct.includes("application/json")) {
26
+ try { data = await res.json(); } catch(e){ data = {ok:false, error:`Invalid JSON: ${e.message}`}; }
27
+ } else {
28
+ const text = await res.text(); // ← Internal Server Error 等をそのまま拾う
29
+ data = {ok:false, error:`HTTP ${res.status} ${res.statusText}: ${text.slice(0,500)}`};
30
+ }
31
  return { status: res.status, data };
32
  }
33
 
 
50
  try{
51
  const fd = new FormData();
52
  for(const f of files){ fd.append("files", f, f.name); }
53
+ const {data} = await postForm("/ingest/upload", fd);
 
54
  if(data.ok){ out.textContent = `保存:${data.saved?.length||0} / チャンク:${data.ingested_chunks}`; }
55
  else { out.textContent = "エラー: " + (data.error || "unknown"); }
56
  log("[UPLOAD] " + JSON.stringify(data));