Spaces:
Runtime error
Runtime error
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | |
<title>Edit and Save Text</title> | |
</head> | |
<body> | |
<h1>Edit and Save Text to JSONBlob</h1> | |
<textarea id="textEditor" rows="20" cols="80"></textarea> | |
<br> | |
<button onclick="saveText()">Save</button> | |
<script> | |
const jsonBlobUrl = 'https://jsonblob.com/api/jsonBlob/5226571730043f8b22dadc20'; | |
// Function to fetch and display the current text from the JSONBlob | |
async function loadText() { | |
try { | |
const response = await fetch(jsonBlobUrl); | |
const data = await response.json(); | |
document.getElementById('textEditor').value = data.text || ''; | |
} catch (error) { | |
console.error('Error fetching the text:', error); | |
} | |
} | |
// Function to save the edited text back to the JSONBlob | |
async function saveText() { | |
const newText = document.getElementById('textEditor').value; | |
const updatedData = { text: newText }; | |
try { | |
const response = await fetch(jsonBlobUrl, { | |
method: 'PUT', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify(updatedData) | |
}); | |
if (response.ok) { | |
alert('Text successfully saved!'); | |
} else { | |
alert('Failed to save text. Please try again.'); | |
} | |
} catch (error) { | |
console.error('Error saving the text:', error); | |
} | |
} | |
// Load the text when the page is loaded | |
window.onload = loadText; | |
</script> | |
</body> | |
</html> | |