I'm using XMLHttpRequest POST to send data to a jsp. But the jsp is not invoked. Here is my code.
The JS method is called on a button click
<form action="">
<div><input type="button" value="POST DATA"
onclick=test();
/></div></form>
JS method to do a http POST -
<script>
function test() {
var xmlHttpRequest = new ActiveXObject('Msxml2.XMLHTTP');
xmlHttpRequest.open("post",
"http://localhost:4502/content/myTest.html", true);
xmlHttpRequest.setRequestHeader("Content-Type",
"application/x-www-form-urlencoded");
xmlHttpRequest.onreadystatechange = function() {
getResult(xmlHttpRequest)
};
try {
xmlHttpRequest.send("username=john");
} catch (e) {
alert("error" + e);
}
}
function getResult(xmlHttpRequest) {
if (xmlHttpRequest.readyState == 4) {
if (xmlHttpRequest.status == 200) {
alert("ok");
} else {
alert("error" + xmlHttpRequest.status);
}
}
}
</script>
myTest.jsp to write POST request to a text file -
<%
FileOutputStream fos = new FileOutputStream("D:/test.txt");
PrintWriter pw = new PrintWriter(fos);
pw.println(request.getParameter("username"));
%>
myTest.jsp is not getting invoked but i get the OK alert. If i try http get instead of post and append parameters to the uri, it works. I'm using IE8.
Pls help.