I have Apache set on my home pc.
- A page called ajax.html has xmlhttp request set on a button. (for fetching a html page main.html residing in same directory)
- Whenever i click on button, nothing happens, i have set response as text.
- Below is javascript code.
function()
{ var httpRequest; document.getElementById("ajaxButton").onclick = function() { makeRequest('main.html'); };function makeRequest(url) { if (window.XMLHttpRequest) { httpRequest = new XMLHttpRequest(); } else if (window.ActiveXObject) { try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); }
catch (e) { try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); }
catch (e) {} } }if (!httpRequest) { alert('Giving up :( Cannot create an XMLHTTP instance'); return false; } httpRequest.onreadystatechange = alertContents; httpRequest.open('GET', url); httpRequest.send(); }
function alertContents() { if (httpRequest.readyState === 4) { if (httpRequest.status === 200) { alert(httpRequest.responseText); } else { alert('There was a problem with the request.'); } } } })();
html code;
<html>
<head>
<title>Ajax Demo</title>
</head>
<body>
<input type="button" id="ajaxButton" value="Click" />
<script type="text/javascript" src="ajax.js">
</script>
</body>
</html>