mirror of
https://github.com/Alvin-Zilverstand/pokedex.git
synced 2026-03-07 05:48:19 +01:00
Improve error handling in API and enhance JSON parsing in script.js
This commit is contained in:
@@ -10,7 +10,7 @@ $conn = new mysqli($servername, $username, $password, $dbname);
|
|||||||
// Check connection
|
// Check connection
|
||||||
if ($conn->connect_error) {
|
if ($conn->connect_error) {
|
||||||
error_log("Connection failed: " . $conn->connect_error);
|
error_log("Connection failed: " . $conn->connect_error);
|
||||||
die("Connection failed: " . $conn->connect_error);
|
die(json_encode(['error' => "Connection failed: " . $conn->connect_error]));
|
||||||
}
|
}
|
||||||
|
|
||||||
header('Content-Type: application/json');
|
header('Content-Type: application/json');
|
||||||
|
|||||||
@@ -4,10 +4,18 @@ document.addEventListener('DOMContentLoaded', () => {
|
|||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
throw new Error('Network response was not ok');
|
throw new Error('Network response was not ok');
|
||||||
}
|
}
|
||||||
return response.json();
|
return response.text().then(text => {
|
||||||
|
try {
|
||||||
|
return JSON.parse(text);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('JSON parse error:', error, text);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
.then(pokemons => {
|
.then(pokemons => {
|
||||||
const pokemonList = document.getElementById('pokemon-list');
|
const pokemonList = document.getElementById('pokemon-list');
|
||||||
|
pokemonList.innerHTML = '';
|
||||||
pokemons.forEach(pokemon => {
|
pokemons.forEach(pokemon => {
|
||||||
const card = document.createElement('div');
|
const card = document.createElement('div');
|
||||||
card.className = 'pokemon-card';
|
card.className = 'pokemon-card';
|
||||||
|
|||||||
Reference in New Issue
Block a user