gpt4 book ai didi

javascript - 如何使用ajax传递单个参数并获取多个值?

转载 作者:行者123 更新时间:2023-12-02 12:38:53 26 4
gpt4 key购买 nike

在我的java项目中,我使用jsp页面。这里我只传递一个参数,我想从数据库中检索多个(3)值。我知道如何仅传递和获取单个参数。但我不知道如何通过传递单个参数来获取多个值。我的ajax代码如下:

var request;
function sendInfo1() {
var v = document.Example.wayid.value;
// var url = 'Showroutelink.jsp?val='+v+'&start='+v1+'&end='+v2;
var url = 'Showroutelink.jsp?val='+v;

if (window.XMLHttpRequest) {

request = new XMLHttpRequest();
} else if (window.ActiveXObject) {
request = new ActiveXObject("Microsoft.XMLHTTP");
}

try {

request.onreadystatechange = getInfo1;
request.open("GET", url, true);
request.send();

} catch (e) {
alert("Unable to connect to server");
}
}

function getInfo1() {

if (request.readyState == 4) {
var val = request.responseText.split('|');



document.getElementById("start").innerHTML = val[0];
document.getElementById("end").innerHTML = val[1];
document.getElementById("waypoints").innerHTML = val[2];
}
}




This is My Setver side Java code

<%
String R_Name=request.getParameter("val");
int w_id=0;


String wplist = "";
// String R_Name = request.getParameter("way");

try {

String buffer = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select start from route where Route_Name='" + R_Name + "'");

while (rs.next()) {
String Startval = rs.getString("start");

buffer = buffer + "<option value='" + Startval + "'>" + Startval + "</option>";


}
buffer = buffer + "</select>";
response.getWriter().println(buffer);

} catch (Exception e) {

}




try {

String buffer1 = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st = con.createStatement();

ResultSet rs = st.executeQuery("select end from route where Route_Name='" + R_Name + "'");

while (rs.next()) {

String Endval = rs.getString("end");

buffer1 = buffer1 + "<option value='" + Endval + "'>" + Endval + "</option>";

}
buffer1 = buffer1 + "</select>";
response.getWriter().println(buffer1);
} catch (Exception e) {

}




try {

String buffer2 = "<select name='state'>";
Class.forName("com.mysql.jdbc.Driver");
java.sql.Connection con1 = DriverManager.getConnection("jdbc:mysql://localhost/googlemap", "root",
"root");
Statement st1 = con1.createStatement();

ResultSet rs1 = st1.executeQuery("select wplist from route where Route_Name='" + R_Name + "'");
while (rs1.next()) {
wplist = rs1.getString("wplist");

String wplist1[] = wplist.split("/");
buffer2 = buffer2 + "<option value='" + wplist1 + "'>" + wplist1 + "</option>";

}
buffer2 = buffer2 + "</select>";
response.getWriter().println(buffer2);
} catch (Exception e) {

}


%>

最佳答案

如果我的问题是正确的,那么您是在问如何在请求中添加多个查询参数。

查询参数的结构如下:

Showroutelink.jsp?key_1=value_1&key_2=value_2&key_n=value_n

所以你只需遵循这个模式即可。

<小时/>

如果您询问如何从服务器调用返回多个值,请尝试使用 JSON。

关于javascript - 如何使用ajax传递单个参数并获取多个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45029544/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com