2.1 KiB
2.1 KiB
Fix: Failed to fetch di Localhost
Masalah
Error: "Failed to fetch" saat mengakses http://localhost:8080/
Backend API error:
[404]: OPTIONS /auth/v1/login - No such file or directory
Penyebab
PHP built-in server dijalankan TANPA router.php, sehingga routing tidak bekerja.
Solusi
1. Jalankan Backend API dengan Router
SALAH (yang menyebabkan error):
cd api-btekno
php -S localhost:8000
BENAR (yang harus dilakukan):
cd api-btekno/public
php -S localhost:8000 router.php
File router.php diperlukan untuk handle routing seperti /auth/v1/login.
2. Jalankan Frontend
cd "retribusi (frontend)/public"
php -S localhost:8080
3. Test Backend API
Buka browser dan akses:
http://localhost:8000/health
Harusnya return: {"status":"ok","time":...}
Jika masih 404, berarti router.php tidak bekerja.
4. Cek Config Frontend
File public/dashboard/js/config.js harus return:
return 'http://localhost:8000';
Quick Start Script
Buat file start-local.bat di root project:
@echo off
echo Starting Backend API...
start "Backend API" cmd /k "cd api-btekno\public && php -S localhost:8000 router.php"
timeout /t 2
echo Starting Frontend...
start "Frontend" cmd /k "cd retribusi (frontend)\public && php -S localhost:8080"
echo.
echo Backend API: http://localhost:8000
echo Frontend: http://localhost:8080
echo.
pause
Jalankan start-local.bat untuk start kedua server sekaligus.
Troubleshooting
Masih 404 di Backend?
- Pastikan menjalankan dari folder
api-btekno/public - Pastikan menggunakan
router.php:php -S localhost:8000 router.php - Cek file
router.phpada diapi-btekno/public/router.php
Masih "Failed to fetch"?
- Buka browser console (F12)
- Cek log
[API] Request:- pastikan URL benar - Test backend langsung:
curl http://localhost:8000/health - Cek CORS - pastikan backend allow origin
http://localhost:8080
CORS Error?
Backend sudah ada CORS middleware, tapi pastikan:
- Backend running dengan router.php
- CORS middleware sudah di-register di
public/index.php