我在JSP页面中有这个JavaScript函数
function loadXMLDoc (voteType){
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp= new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function (){
if (xmlhttp.readyState == 4){
alert ('arriva qua');
if (xmlhttp.status = 200){
alert (xmlhttp.responseText);
if ('ALREADY_VOTED_UP' == xmlhttp.responseText) {
alert ('You already voted up for this question');
} else if ('VOTING_SUCCESSFULL' == xmlhttp.responseText) {
if (voteType=='UP'){
var total = parseInt(document.getElementById('upvoteCount').innerHTML);
document.getElementById('upvoteCount').innerHTML = total + 1;
}
}
else {
alert ('Your vote can not be saved');
}
}
}
};
xmlhttp.open("POST", "VotingAnswer");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send ("answerId=${answer.id}" + "questionId=${question.id}&type="+voteType);
}
</script>
使用此按钮我将数据发送到servlet
<button type="submit"><img width="20px" height="20px" src="assets/img/Hands-Thumb-Up-icon.png"></img></button>
这是VotingAnswer
servlet
Long answerId = 0L;
Long questionId = 0L;
questionId = Long.parseLong(request.getParameter("questionId"));
answerId = Long.parseLong(request.getParameter("answerId"));
但是它给了我一个与servlet中的answer.id参数相关的错误
java.lang.NumberFormatException:
表示我没有与此参数相关的值
所以,如果我只在xmlhttp.send
中放入2个参数,那么一切正常
xmlhttp.send ("questionId=${question.id}&type="+voteType);
我做错了什么?
似乎我没有与参数answer.id
相关联的数据,但这不是真的,因为如果我使用jsp按钮并且表单一切正常......它只是JavaScript语法的问题也许,我真的不知道如何解决它。