I am seeking to develop a unique function for AJAX post requests.
Below is the existing source code:
function handleAjax(url, data) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
return xhr.responseText;
}
};
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhr.send(data);
}
console.log(handleAjax('shipment.php', 'fname=test1&lname=test2'));
However, there seems to be an issue as the console displays "undefined".
If I replace `return` with `alert()` it works fine. So, why isn't `return` working in this case?