103 lines
2.1 KiB
Markdown
103 lines
2.1 KiB
Markdown
|
|
# 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):
|
||
|
|
```bash
|
||
|
|
cd api-btekno
|
||
|
|
php -S localhost:8000
|
||
|
|
```
|
||
|
|
|
||
|
|
**BENAR** (yang harus dilakukan):
|
||
|
|
```bash
|
||
|
|
cd api-btekno/public
|
||
|
|
php -S localhost:8000 router.php
|
||
|
|
```
|
||
|
|
|
||
|
|
File `router.php` diperlukan untuk handle routing seperti `/auth/v1/login`.
|
||
|
|
|
||
|
|
### 2. Jalankan Frontend
|
||
|
|
|
||
|
|
```bash
|
||
|
|
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:
|
||
|
|
```javascript
|
||
|
|
return 'http://localhost:8000';
|
||
|
|
```
|
||
|
|
|
||
|
|
## Quick Start Script
|
||
|
|
|
||
|
|
Buat file `start-local.bat` di root project:
|
||
|
|
|
||
|
|
```batch
|
||
|
|
@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?
|
||
|
|
|
||
|
|
1. Pastikan menjalankan dari folder `api-btekno/public`
|
||
|
|
2. Pastikan menggunakan `router.php`: `php -S localhost:8000 router.php`
|
||
|
|
3. Cek file `router.php` ada di `api-btekno/public/router.php`
|
||
|
|
|
||
|
|
### Masih "Failed to fetch"?
|
||
|
|
|
||
|
|
1. Buka browser console (F12)
|
||
|
|
2. Cek log `[API] Request:` - pastikan URL benar
|
||
|
|
3. Test backend langsung: `curl http://localhost:8000/health`
|
||
|
|
4. Cek CORS - pastikan backend allow origin `http://localhost:8080`
|
||
|
|
|
||
|
|
### CORS Error?
|
||
|
|
|
||
|
|
Backend sudah ada CORS middleware, tapi pastikan:
|
||
|
|
1. Backend running dengan router.php
|
||
|
|
2. CORS middleware sudah di-register di `public/index.php`
|
||
|
|
|