AJAX Post不发送Params

时间:2016-06-02 16:38:49

标签: javascript ajax

Req:在使用struts / velocity

的Web应用程序中

JavaScript需要将参数传递给Java类中的方法。我使用AJAX post调用servlet。但是无法在动作类中接收参数。

JavaScript功能

funtion posttoservlet(){
var id=2
var param="Count="+id;
var xmlhttp= new XMLHttpRequest();
xmlhttp.open("POST","DataServlet.action",true);
xmhttp.setrequestheader('contenttype','plain/text');
xmlhttp.send(param);
}

struts.xml

<action name="DataServlet" method="getfromjs" class=com.test.servletpost>
</action>

servletpost.java

public void getfromjs(){

syso(servletactioncontext.getrequest().getparameter("Count")); // This is printing null instead of printing "2". Please advise.
}

我很困惑为什么参数没有正确发布

1 个答案:

答案 0 :(得分:0)

setRequestHeader错了:

xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');

当前代码,不设置请求标头。此无效语法也可能会引发错误。试试以上。请注意,JavaScript是CaSe SeNsItIvE。