This question already has an answer here:
I am writing some JavaScript code so that when a user clicks a button, a list of something appears as a JavaScript alert.
I have written an AJAX request using XMLHttpRequest. However, when I press the button, undefined appears in the alert box. I expected the list from the php file to be displayed in the alert box. Below is the code I've written:
<!DOCTYPE html>
<head>
<body>
<h1>Avengers Characters Search</h1>
<button onclick = "search()"> Search </button>
<script type="text/javascript" src="app.js"></script>
</body>
</html>
JavaScript Code:
function search(showmethemoney) {
alert(showmethemoney);
}
function showmethemoney() {
var x = new XMLHttpRequest();
x.responseType = 'php';
x.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("search").onclick = this.responseText;
}
};
x.open("GET", "superheroes", true);
x.send();
}