In my web application using Struts and Velocity, I'm facing an issue where JavaScript needs to pass a parameter to a method in a Java class. I have tried using an AJAX post call to a servlet, but I am not able to receive the parameter in the action class.
JavaScript function:
function postToServlet(){
var id = 2;
var param = "Count=" + id;
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("POST", "DataServlet.action", true);
xmhttp.setRequestHeader('content-type', 'plain/text');
xmlhttp.send(param);
}
struts.xml
<action name="DataServlet" method="getFromJS" class="com.test.ServletPost" />
Servletpost.java
public void getFromJS() {
System.out.println(ServletActionContext.getRequest().getParameter("Count")); // This outputs null instead of "2". Please advise.
}
I'm puzzled as to why the parameters are not being posted properly.