【发布时间】:2016-11-02 00:58:47
【问题描述】:
我想使用 request.getparameter 来确定我的页面事件的操作。示例添加或删除按钮。
但是当我有按钮时 onsubmit="post();return false;" ajax 的 Javascript 返回内容类型 application/json request.get 参数始终为 null。
当我删除 onsubmit 方法时,我会得到请求参数等于“add”。
在这种情况下,我应该如何确定操作,以便我可以在页面上拥有不同的按钮。
JavaScript
function post() {
var xhttp;
if (window.XMLHttpRequest) {
// code for modern browsers
xhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.onreadystatechange = function () {
if (xhttp.readyState == 4 && xhttp.status == 200) {
document.getElementById("postView").innerHTML = xhttp.responseText;
}
};
//Do someting
xhttp.open("POST", "myServlet", true);
xhttp.setRequestHeader("Content-Type", "application/json");
xhttp.send(jsonObj);
}
HTML Form
<form name="myForm" action="myServlet" method="post" onsubmit="post();return false;">
Name: <input type="text" id="fnText">
<input type="submit" name="add" value="Add" />
</form>
Java Servlet
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("Method "+request.getMethod());
String add = request.getParameter("add");
System.out.println("getParameter is " + add);
}
【问题讨论】:
标签: javascript java json ajax servlets