我是jsp
和ajax
的新手。如何在javascript中使用ajax将多个变量从jsp传递到servlet
如何将多个变量传递给servlet使用xmlhttp.open("GET",servlet,false);
从ajax
在jsp
。
我有两个选择框,如名称和电话号码,我需要将选定的值发送给servlet,在servlet中我使用ajax将地址,城市等多个详细信息传递给jsp。
我是jsp
和ajax
的新手。如何在javascript中使用ajax将多个变量从jsp传递到servlet
如何将多个变量传递给servlet使用xmlhttp.open("GET",servlet,false);
从ajax
在jsp
。
我有两个选择框,如名称和电话号码,我需要将选定的值发送给servlet,在servlet中我使用ajax将地址,城市等多个详细信息传递给jsp。
如果你想使用GET,你可以通过通过URL变量进行编码,并将它们添加到URL请求,就像这样:
var url = "/path/to/myservlet";
var params = "somevariable=somevalue&anothervariable=anothervalue";
var http = new XMLHttpRequest();
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function()
{
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(null);
如果你有长DATAS通过,使用POST是首选方式,这里就是这种代码的一个例子:
var http = new XMLHttpRequest();
var url = "/path/to/myservlet";
var params = "lorem=ipsum&name=binny";
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
您将能够使用getParameter(String name)
方法在servlet HttpServletRequest
阅读这些DATAS。
希望这会有所帮助。 :-)
感谢乌尔回复(xmlhttp.open( “GET”, “ClientDetails” + “?客户=” + documnt.getElementById( “客户端”)。值+ “&接触=” + documnt.getElementById ( “联系人”)值,假); xmlhttp.send();如果 (xmlhttp.readyState == 4){ 如果(xmlhttp.status == 200){ 警报 (xmlhttp.responseText); document.getElementById(“address”)。value = xmlhttp.responseText; document.getElementById(“city”)。value = xmlhttp.responseText; } }这是我的脚本请检查一下是对还是错 – Cinthiyal