I have a server side javascript file index.js, running locally using nodejs:
function myFunction() {
console.log("It works!");
}
I would like to call this function from client side using Ajax:
<body
<button id="mybtn" onclick="callMyFunction()">Call function</button>
<script>
function callMyFunction() {
// Creating Our XMLHttpRequest object
var url = '/index.js';
var xhttp = new XMLHttpRequest();
// Making our connection
xhttp.open("GET", url, true);
// function execute after request ist successful
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
console.log(this.responseText)
// What to do here?
}
};
// Sending our request
xhttp.send();
}
</script>
</body>
I don't know how to properly call that function?
Later this function should run on a webserver and every client should be able to call that function.