Question: please convert it to a fetch function function function handleSubmitButton() { let userText = document.getElementById('userTextField').value if (userText && userText != '') { let userRequestObj =
please convert it to a fetch function function
function handleSubmitButton() {
let userText = document.getElementById('userTextField').value
if (userText && userText != '') {
let userRequestObj = {text: userText}
let userRequestJSON = JSON.stringify(userRequestObj)
document.getElementById('userTextField').value = ''
//alert ("You typed: " + userText);
let xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log("data: " + this.responseText)
console.log("typeof: " + typeof this.responseText)
//we are expecting the response text to be a JSON string
let responseObj = JSON.parse(this.responseText)
movingString.word = responseObj.text
drawCanvas()
}
}
xhttp.open("POST", "userText") //API .open(METHOD, URL)
xhttp.send(userRequestJSON) //API .send(BODY)
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
