3👍
✅
Response resulting from fetch()
does not have a responseText
property, hence the undefined
. You can extract the JSON data from the response using method json()
on the the response. responseText exists with XMLHttpRequest
, but not with fetch()
:
fetch("validate-recaptcha.php", {
method: "post",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({ name: "myName", password: "myPassword" })
})
.then((response) => {
if (response.status == 200) {
alert(response.statusText);
}
else {
alert("Error: " + response.statusText);
}
/* returns a promise that can be utilized using `then() */
return response.json();
// could also use then() here
// return response.json().then(data => console.log(data));
})
.then(data => console.log(data));
Hopefully that helps!
Source:stackexchange.com