iteye_20587 2009-10-22 11:09
浏览 370
已采纳

ajax请求三次为什么就无效了

用ajax 做个二级联动的菜单,改变第一个下拉框中的值,第二个下拉框跟着改变,前三次请求能正确完成,三次以上则不行,在网上搜的答案都试过了,附jsp和servlet在下面。

package com.pynon.crawler;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.pynon.base.util.MysqlConnections;

public class subtype extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
    
        response.setContentType("text/xml");
    
        response.setHeader("Cache-Control", "no-cache");
        PrintWriter out = response.getWriter();
        //String supertype = new String(request.getParameter("supertype").getBytes("ISO-8859-1"),"gbk");
        String supertype=request.getParameter("supertype");
        // System.out.println(supertype);

        List list = new ArrayList();

        Connection conn = MysqlConnections.getConnection();
        if (conn != null) {
            Statement stmt = null;
            ResultSet rs = null;
            try {

                stmt = conn.createStatement();
                rs = stmt
                        .executeQuery("select id,classtype  from dt_talknet where typename='"
                                + supertype + "';");
                while (rs.next()) {
                    Map map = new HashMap();
                    map.put("id", rs.getInt(1));
                    map.put("classtype", rs.getString(2));
                    list.add(map);
                    // System.out.println(rs.getInt(1) + "  " +
                    // rs.getString(2));
                }

            } catch (SQLException e) {
                MysqlConnections.freeConnection(conn);
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                        rs = null;
                    }
                    if (stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                } catch (SQLException e1) {
                    e1.getMessage();
                }
            }
        }
        if (list.size() > 0) {
            out.print("<response>");
            for (int i = 0; i < list.size(); i++) {
                Map map = (Map) list.get(i);
                out.print("<subtypeids>" + map.get("id") + "</subtypeids>");
                out.print("<subtypenames>" + map.get("classtype")
                        + "</subtypenames>");
            }
            out.print("</response>");
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);

    }

}
<body>


<fieldset><script type="text/javascript">
var XMLHttpReq;
    //创建XMLHttpRequest对象              
function createXMLHttpRequest() {
    if(window.XMLHttpRequest) { //Mozilla 浏览器
        XMLHttpReq = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) { // IE浏览器
        try {
            XMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                XMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                }
        }
    }
}
function go4()
{  
    createXMLHttpRequest();
    var supertype = document.getElementById("supertype").value;//获取选中的大类别.
    var url = "/pynon/subtype?supertype="+supertype+"&param="+ new Date().getTime();
    XMLHttpReq.onreadystatechange = processResponse;//指定响应函数
    XMLHttpReq.open("POST",url,true);
    
    XMLHttpReq.setRequestHeader("If-Modified-Since","0"); 
    XMLHttpReq.setRequestHeader("Cache-Control","no-cache");
    XMLHttpReq.send(null);
     
}
function processResponse() {    
    if (XMLHttpReq.readyState == 4) { // 判断对象状态
        if (XMLHttpReq.status == 200 || XMLHttpReq.status==0) { // 信息已经成功返回,开始处理信息
            updateList();
        } else { //页面不正常
            window.alert("您所请求的页面有异常。");
        }
    }
}
function updateList()
{
    clearList();
    var subtype = document.getElementById("subtype");//小类别的控件对象
    var subtypeids = XMLHttpReq.responseXML.getElementsByTagName("subtypeids");
    var subtypenames = XMLHttpReq.responseXML.getElementsByTagName("subtypenames");
    var option=null;
    for(var i=0;i<subtypeids.length;i++)
    {
        option = document.createElement("option");
        option.value = subtypeids[i].firstChild.nodeValue;              
        option.appendChild(document.createTextNode(subtypenames[i].firstChild.nodeValue));
        subtype.appendChild(option);
    }
}
function clearList() {
        
            var subtype = document.getElementById("subtype");
            while(subtype.childNodes.length > 0) {
                subtype.removeChild(subtype.childNodes[0]);
            }
    }
</script>


<div>分析模板:<select id="supertype" onChange="go4()"
    style="width: 100px">
    <option>请选择类别</option>
    <%
        Connection cont = MysqlConnections.getConnection();
            if (cont != null) {
                Statement stmt = null;
                ResultSet rs = null;
                try {
                    stmt = cont.createStatement();
                    rs = stmt
                            .executeQuery("select distinct typename from dt_talknet");

                    while (rs.next()) {
    %>

    <option value="<%=rs.getString("typename")%>"><%=rs.getString("typename")%></option>
    <%
        }
    %>
</select> <%
    if (rs != null) {
                    rs.close();
                    rs = null;
                }
                stmt.close();
                stmt = null;
                MysqlConnections.freeConnection(cont);
            } catch (SQLException e) {
                MysqlConnections.freeConnection(cont);
                e.printStackTrace();
            } finally {
                try {
                    if (rs != null) {
                        rs.close();
                        rs = null;
                    }
                    if (stmt != null) {
                        stmt.close();
                        stmt = null;
                    }
                } catch (SQLException e1) {
                    e1.getMessage();
                }
            }
        }
 %> 
 <select style="width: 200px" id="subtype" name="subtype">
    <option value="0">==请选择==</option>
</select>

</div>
<br>

<br>
<br>
</fieldset>


</body>

 


问题补充:
lovewhzlq 那个我现在加了,这个问题到底是为什么呀,帮我看看呀!谢谢啦
问题补充:
三次都正常执行了,按第四次的时候二级类别的就不动了,还是显示的第三次出来的子类别,如果把浏览器关了,重新来,又是只能执行三次
  • 写回答

3条回答 默认 最新

  • 成尚謙 2009-10-22 13:33
    关注

    [code="java"]
    function go4()

    {

    createXMLHttpRequest();

    var supertype = document.getElementById("supertype").value;//获取选中的大类别.

    var url = "/pynon/subtype?supertype="+supertype+"&param="+ new Date().getTime();

    alert(url);//检查传入的值与url
    XMLHttpReq.onreadystatechange = processResponse;//指定响应函数

    XMLHttpReq.open("POST",url,true);

    XMLHttpReq.setRequestHeader("If-Modified-Since","0");   
    XMLHttpReq.setRequestHeader("Cache-Control","no-cache");  
    XMLHttpReq.send(null);  
    

    }

    [/code]

    在doGet方法第一行打印一些东西
    System.out.println("服务器接收到ajax请求!");

    [quote]if (list.size() > 0) {

    out.print("");

    for (int i = 0; i < list.size(); i++) {

    Map map = (Map) list.get(i);

    out.print("" + map.get("id") + "");

    out.print("" + map.get("classtype")

    + "");

    }

    out.print("");

    }[/quote]
    改为
    [code="java"]
    String results = "";
    if (list.size() > 0) {

    results += "";

    for (int i = 0; i < list.size(); i++) {

    Map map = (Map) list.get(i);

    results += "" + map.get("id") + "";

    results += "" + map.get("classtype")

    + "";

    }

    results += "";
    System.out.println("请求结果:" + results);//跟踪一下返回的数据是否有变化
    out.print(results);
    }
    [/code]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的