5c0f1el9 5c0f1el9 - 3 months ago 20
PHP Question

Ajax won't run on xampp

I am trying to run the following code on xampp :

<! DOCTYPE html>
<html>
<head>
<title>Ajax test</title>
</head>
<body>
<script>
function sendAjax() {
var xmlObject = XMLHttpRequest();
xmlObject.open("GET", "process.php", true);
xmlObject.onreadystatechange = function() {
if (xmlObject.readyState == 4 && xmlObject.status == 200) {
document.getElementById("response").innerHTML = xmlObject.responseText;
}
}
xmlObject.send();
}
</script>
<div id="response"></div>
<input type="button" onclick="sendAjax()" value="Send Ajax Request">
</body>
</html>


Here is the process.php file content :

<?php
echo "Hello wolrd !";
?>


On clicking on "Send Ajax Request button" the "Hello wolrd:" message is not displayed ..

Any help please ?

PS: My project folder contains two files index.php && process.php

Answer

You must declare your object with new operator:

var xmlObject = new XMLHttpRequest();