我完全不熟悉AJAX因此這個問題。我想從我的JavaScript代碼發送一些信息到我的servlet。新手AJAX Qn:request.send(信息)
function getDetails() {
vals = document.getElementById("name").value;//works: vals does get inputted value
request = createRequest();
if (request == null) {
alert("Unable to create request");
return;
}
var url= "ValidateUser.do";
request.open("POST", url, true);
request.onreadystatechange = displayDetails;
//How do I send the value of "vals" to my servlet?
request.send("name="+vals);
}
當我在我的servlet運行req.getParameter(「名稱」),我總是得到即使「丘壑」不包含輸入值沒有價值。所以我的問題是 - 我如何從我的servlet訪問這個值?
編輯:
function createRequest() {
try {
request = new XMLHttpRequest();
} catch (tryMS) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (otherMS) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (failed) {
request = null;
}
}
}
return request;
}
進一步編輯: servlet代碼:我想println語句打印出了名。
//shortened: this method is called by a ControllerServlet
public Object perform(HttpServletRequest req, HttpServletResponse resp) {
//retrieve customer from database
model = SeekerCustomer.getCustomer(req.getParameter("name"));
System.out.println(req.getParameter("name"));
}
你能還添加源代碼的方法 '的createRequest()'? – reporter