i have an SyntaxError: Unexpected end of JSON input and i dont know how to solve that. My method at controller was return a json but why its still error. When i change the method to post it ok but i need it as get , please help me
This my code to fetch endpoint at my laravel 10 controller
async function searchFunction() {
let input, filter;
input = document.getElementById('searchInput');
search = input.value;
try {
// Make a request to the server
const response = await fetch(`/warga/search?search=${search}`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
'X-CSRF-TOKEN': document.querySelector('meta[name="csrf-token"]').content,
},
});
const responseData = await response.json();
clearTable();
responseData.forEach(item => {
addRowToTable(item);
});
} catch (error) {
console.error('Error:', error);
}
}
This is the method at my controller
{
$search = $request->input('search', '');
$warga = Warga::with('alamat')
->where('nama', 'like', "%{$search}%")
->paginate(6);
if ($warga->isEmpty()) {
return response()->json(['error' => 'No results found'], 404);
}
// Return pagination data along with the results
return response()->json([
$warga,
'pagination' => [
'total' => $warga->total(),
'per_page' => $warga->perPage(),
'current_page' => $warga->currentPage(),
'last_page' => $warga->lastPage(),
],
], 200);
}```
This the route
```Route::get('/warga/search', [AppHttpControllersCivilliantController::class, 'search']);
i have try to debug at console and idk why my method return a blank page or somethimes 419 expired
New contributor
rul is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.