Not sure what I am doing wrong here.
HTML
<form id="inp" onsubmit="process()">
<input id="link" type="text" name="link" placeholder="Enter your link">
<input id="submit" type="submit" name="submit" value="→">
</form>
JavaScript
function createRequest() {
let result = null;
if (window.XMLHttpRequest) {
result = new XMLHttpRequest();
result.overrideMimeType('application/json');
}
else {
window.alert("Abort!");
}
return result;
}
let c=0;
function process() {
let l = document.getElementById("inp").link.value;
let resp;
let req = createRequest();
let payload = {
link: l,
ignoreException: true
};
req.open("POST", url, true);
req.setRequestHeader("Content-Type", "application/json");
req.send(payload);
req.onreadystatechange = function () {
if (req.readyState === 4 && req.status === 200) {
resp = req.responseText;
console.log("response text - " + resp);
}
console.log(req.readyState + " " + req.status + " " + c);
};
}
I want it to log the readyState and status for every state change and also log the response text for the final state.
It logs " 4 0 1 " which means it's changing the state just once and is changing it straight to the final state.
It should change the states as .open(), .setRequestHeader(), and .send() functions are executed.
How can I make this work?
Note- "url" parameter is the api url.