While console.log("result ="+req.responseText);
can output actual content, if(response == null) console.log("response null");
gets executed as well.
How can I return http response correctly?
var response = callSvc(textContent);
if(response == null) console.log("response null");
function callSvc(text){
var cleanUrl = "http://localhost:8080/api/mock";
var body={
"text":text,
"user":contentId
}
var contentId = generateContentId();
var req = new XMLHttpRequest();
req.open("POST", cleanUrl);
req.setRequestHeader("Content-Type", "application/json; charset=utf8");
var json = JSON.stringify(body);
req.send(json);
req.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
var data = JSON.parse(req.responseText);
console.log("result ="+req.responseText);
return data;
} else {
return error => {
console.error(error);
}
}
}
}