-2
I’m using the function fetch
down below:
var url = "https://jsonplaceholder.typicode.com/users/2";
fetch(url).then(function(response) {
if (!response.ok) {
alert("Response not ok" + response.statusText);
throw new Error(response.statusText);
} else {
alert(" Response Swal Json:" + response.json());
}
return response.json();
}).catch(function(error) {
alert("DeuErro -> " + error);
swal.showValidationError(
'Erro: ' + error
)
})
But she always makes the mistake:
Typeerror: Body has already been Consumed.
What could be going on? What ways to correct?
Note: In the modal of the snippet you used to enter the code there is the "Organize" button. Use it to properly format the codes before creating the question to make it easier to read the codes.
– Woss
Thanks for the @Woss tip, I didn’t know this button
– Tiago Ferezin
Within the
else
, you already consume the body of the answer when you doresponse.json()
; when you try to return the same value, there will no longer be content to be consumed. Why not assign this to a variable and reuse it in these two places?– Woss