30 lines
944 B
JavaScript
30 lines
944 B
JavaScript
function requestJsonFromServer(route) {
|
|
return fetch(route)
|
|
.then(response => {
|
|
if (!response.ok) {
|
|
throw new Error('Network response was not ok');
|
|
}
|
|
return response.json(); // Assuming the server returns a JSON response
|
|
})
|
|
.then(jsonData => {
|
|
//console.log('Received JSON data:', jsonData);
|
|
return jsonData; // Return the JSON data
|
|
})
|
|
.catch(error => {
|
|
console.error('There was a problem with the fetch operation:', error);
|
|
return null; // Return null in case of an error
|
|
});
|
|
}
|
|
|
|
/* Example usage
|
|
requestJsonFromServer('/get_data')
|
|
.then(data => {
|
|
if (data !== null) {
|
|
console.log("Data received:", data);
|
|
// You can process the data here as needed
|
|
} else {
|
|
console.log("Failed to retrieve data from the server.");
|
|
}
|
|
});
|
|
*/
|