Java jQueryAjax调用从mySQL筛选值。

Java jQueryAjax调用从mySQL筛选值。,java,javascript,jquery,ajax,jsp,Java,Javascript,Jquery,Ajax,Jsp,我想发送click radio和server获取的queryString,以获得xml响应。我几乎接近解决方案,无法调试问题为什么它不工作 <script> $(document).ready( function() { $("#myCity").click( /* $("input[type=radio][name=myCity]").click( */

我想发送click radio和server获取的queryString,以获得xml响应。我几乎接近解决方案,无法调试问题为什么它不工作

<script>

$(document).ready(
                function() {
                       $("#myCity").click(  
                    /* $("input[type=radio][name=myCity]").click( */
                                       function() {
                                                 var radioCity = $('input:radio[name=myCity]:checked').val(); 
                                              /* radioCity = $('input[name=myCity]').filter(':checked').value();  */

                                              link = "http://localhost:8080/Shipping_Order/getCity_xml.jsp?qString="+radioCity;

                                        $.ajax({ 
                                            type:"GET",
                                            url : link,
                                            data : radioCity,
                                            dataType : "xml",
                                            success : function() {

                                                /* var myCity = $('input:radio[name=myCity]:checked').value; */
                                                /* var myCity = $('input[name=myCity]:radio:checked').val() */

                                                for ( var i = 0; i < xmlDoc.getElementsByTagName("city").length; i++) {

                                                    $("#radioTable").append(
                                                                    '<tr><td id="username"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("username")[i].childNodes[0].nodeValue
                                                                            + '</td><td id="city"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("city")[i].childNodes[0].nodeValue
                                                                            + '</td> <td id="contact"'+i+'>'
                                                                            + xmlDoc
                                                                                    .getElementsByTagName("contact")[i].childNodes[0].nodeValue
                                                                            + '</td></tr>');

                                                }
                                            }

                                        });
                                    });
                });

$(文件)。准备好了吗(
函数(){
$(“#我的城市”)。点击(
/*$(“输入[type=radio][name=myCity]”。单击(*/
函数(){
var radioCity=$('input:radio[name=myCity]:选中').val();
/*radioCity=$('input[name=myCity]')。筛选器(':checked')。值()*/
链接=”http://localhost:8080/Shipping_Order/getCity_xml.jsp?qString=“+放射性;
$.ajax({
键入:“获取”,
网址:link,
数据:放射性,
数据类型:“xml”,
成功:函数(){
/*var myCity=$('input:radio[name=myCity]:checked')。值*/
/*var myCity=$('input[name=myCity]:radio:checked').val()*/
对于(var i=0;i

在jQuery的click事件中,变量“radioCity”获取当前的无线电值,ajax“url”通过get request发送该值,并与queryString一起传递。服务器(getCity_xml.jsp)以xml数据响应请求,稍后它将通过选择器名称#radioTable追加该值

用我创建的HTML

getCity_xml.jsp

<%
response.setContentType("text/xml");

try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/shipping_order","root","root");
Statement st = con.createStatement();
String myCity = request.getParameter("qString");
ResultSet rs = st.executeQuery("select username, contact, city from user where city ="+myCity);

while(rs.next())
{

    out.println("<user>");
    out.println("<username>"    +rs.getString(1)+   "</username>");
    out.println("<contact>"     +rs.getInt(2)+      "</contact>");
    out.println("<city>"        +rs.getString(3)+   "</city>");
    out.println("</user>"); 

}


rs.close();
st.close();

con.close();

}catch (SQLException ex) {out.println("Exception Occured");}  

%>

只是一个旁注。 永远不要!!!111在SQL查询中使用字符串连接

这样做会给代码引入SQL注入漏洞。 考虑访问本教程:

至于这个问题,你应该用引号将城市名称转义

ResultSet rs = st.executeQuery("select username, contact, city from user where city = '"+myCity + "'");

您遇到了什么错误?另外,我看到的一个问题(可能不是问题的原因)是,您需要转义XML,而不是仅使用rs.getString(1)和rs.getString(3)。如果您的字符串碰巧包含一个“您不需要在使用它之前定义xmlDoc吗?例如“success:function(xmlDoc)”而不是“success:function()“谢谢,我找到了解决方案。感谢您的解决方案。