From e53a6babdb12145bd0fd2dcb9300f197a71b2945 Mon Sep 17 00:00:00 2001 From: Jukka Lampikoski Date: Wed, 11 Mar 2026 13:52:50 +0200 Subject: [PATCH] =?UTF-8?q?Debug:=20paranna=20virhek=C3=A4sittely=C3=A4=20?= =?UTF-8?q?todo=5Fstatus=20+=20apiCall=20JSON=20parse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit apiCall palauttaa nyt selkeän virheen jos palvelimen vastaus ei ole validia JSON:ia (+ logittaa console.error). todo_status palauttaa JSON-virheen myös edge-caseissa. Co-Authored-By: Claude Opus 4.6 --- api.php | 6 ++++-- script.js | 7 ++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/api.php b/api.php index f6954aa..e6b7bd1 100644 --- a/api.php +++ b/api.php @@ -2289,8 +2289,10 @@ switch ($action) { case 'todo_status': requireAuth(); $companyId = requireCompany(); - if ($method !== 'POST') break; - $input = json_decode(file_get_contents('php://input'), true); + if ($method !== 'POST') { echo json_encode(['error' => 'POST required']); break; } + $rawInput = file_get_contents('php://input'); + $input = json_decode($rawInput, true); + if (!$input) { echo json_encode(['error' => 'Invalid JSON input', 'raw' => substr($rawInput, 0, 200)]); break; } $id = $input['id'] ?? ''; $status = $input['status'] ?? ''; $todo = dbLoadTodo($id); diff --git a/script.js b/script.js index 90eb028..9ef34bf 100644 --- a/script.js +++ b/script.js @@ -32,7 +32,12 @@ async function apiCall(action, method = 'GET', body = null) { opts.body = JSON.stringify(body); } const res = await fetch(`${API}?action=${action}`, opts); - const data = await res.json(); + const text = await res.text(); + let data; + try { data = JSON.parse(text); } catch (e) { + console.error('API JSON parse error:', action, text.slice(0, 500)); + throw new Error('Palvelin palautti virheellisen vastauksen'); + } if (!res.ok) throw new Error(data.error || 'Virhe'); return data; }