This code wont execute. I think there is something wrong with the xhttp.send()
function. Because the alert
function before it executes but the alert
function after it doesn't execute:
<html>
<head>
<title>PAGE OUTPUT</title>
<script type="text/javascript">
function aa()
{
var xhttp;
alert("hi welcome");
if(window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
alert("hi");
}
else
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","vehicle.xml",false);
alert("OPEN EXECUTED");
xhttp.send();
alert("SEND EXECUTED");
xmlDoc=xhttp.responseXML;
alert("HI I HAVE REACHED OVER HERE");
var vehicle=xmlDoc.documentElement;
var car=vehicle.firstChild.nodeValue;
var price=car.firstChild.nodeValue;
alert(price);
}
</script>
</head>
<body>
<center><input type="submit" onclick="aa()"></center>
</body>
You'd better to check if the URL is like file:///path/to/file the browser will fail to send any XHR from the page, for security reasons (nowadays that seems unreasonable).
You should launch a server for this case, and visit that page in http://localhost/path/to/file. The easiest way I know is to launch a Python SimpleHTTPServer. You might install python yourself if you are using MS Windows, or it will be available in Mac/any Linux distributions. Then follow these steps
cmd
to launch one)cd
to the path where the files locatepython -m SimpleHTTPServer
http://localhost:8000/html_file
in your browserNow you will see those alert
s. Additionally, you might have to change xmlDoc=xhttp.responseXML
to xmlDoc = xhttp.responseText
if necessary.
Then try jQuery, that's much easier for ajax.