I am a beginner in java script. And I have the following script
function sendImageToBackend(formData) {
fetch('my-back-end-url', {
method: 'POST',
body: formData,
})
.then((response) => {
if (!response.ok) {
throw new Error('Error uploading image');
}
return response.json(); // Parse the response as JSON
})
.then((data) => {
// Handle the received data from the backend
displayResponseData(data);
})
.catch((error) => {
console.error('Error uploading image:', error);
// Display an error message to the user
alert('Error uploading image. Please try again later.');
});
}
function displayResponseData(data) {
responseContainer.innerHTML = '';
const heading = document.createElement('h2');
heading.textContent = 'Response Data:';
responseContainer.appendChild(heading);
const dataParagraph = document.createElement('p');
dataParagraph.textContent = JSON.stringify(data);
responseContainer.appendChild(dataParagraph);
}
});
So my Back-End accepts an image and it takes some time to process it. And the Back-End will return an image to the front-end. I did test my back-end (an FastAPI on Cloud Run) and it works, but I have problem with java script. Do you know how to fix it? So I want to display the image from the backend.