我在JSP页面中写道:
var sel = document.getElementById("Wimax");
var ip = sel.options[sel.selectedIndex].value;
var param;
var url = 'ConfigurationServlet?ActionID=Configuration_Physical_Get';
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
httpRequest.open("POST", url, true);
httpRequest.onreadystatechange = handler(){
if (httpRequest.readyState == 4) {
if (httpRequest.status == 200) {
param = 'ip='+ip;
param += 'mmv='+mmv;
param += "tab="+tab;
}};
httpRequest.send(param);我希望在我的param中使用这个ConfigurationServlet变量。有人能告诉我如何在servlet中获得这个json对象吗?
更新:我更改了语句,现在它将状态代码显示为200。
var index = document.getElementById("Wimax").selectedIndex;
var ip = document.getElementById("Wimax").options[index].text;
httpReq = GetXmlHttpObject();
alert(httpReq);
var param = "ip=" + ip;
param += "&mmv=" + mmv;
param += "&tab=" + tab;
alert("param "+param);
var url="http://localhost:8080/WiMaxNM/ConfigurationServlet?ActionID=Configuration_Physical_Get";
url = url+"?"+param;
httpReq.open("GET",url,true);
alert("httpReq "+httpReq);
httpReq.onreadystatechange = handler;
httpReq.send(null);但也出现了新的问题。控件根本不转到url中指定的servlet操作ID。请告诉我这里出了什么问题。
发布于 2010-05-04 11:18:52
只有在发送请求之后才会调用处理程序中的代码。在此之前,您需要填充param。您还需要通过&连接单独的参数。
因此,例如。
// ...
httpRequest.onreadystatechange = handler() {
// Write code here which should be executed when the request state has changed.
if (httpRequest.readyState == 4) {
// Write code here which should be executed when the request is completed.
if (httpRequest.status == 200) {
// Write code here which should be executed when the request is succesful.
}
}
};
param = 'ip=' + ip;
param += '&mmv=' + mmv;
param += "&tab=" + tab;
httpRequest.send(param);然后,您可以通过servlet以通常的HttpServletRequest#getParameter()方式访问它们。
尽管如此,您在那里发布的Ajax代码只会在中工作,而不会在全世界所知道的其他四个主要not浏览器中工作。换句话说,你的Javascript代码对世界上大约一半的人来说是行不通的。
我建议看一看jQuery,以减少所有冗长的工作,并消除跨浏览器兼容性问题。所有的代码都可以很容易地被替换成
var params = {
ip: $("Wimax").val();
mmv: mmv,
tab: tab
};
$.post('ConfigurationServlet?ActionID=Configuration_Physical_Get', params);并且仍然在所有的网页浏览器中工作!
更新:根据您的更新,最终的是完全错误的。?表示查询字符串的start。您的URL中已经有一个了。您应该使用&链接查询字符串中的参数。也就是说。
url = url + "&" + param;https://stackoverflow.com/questions/2763805
复制相似问题