日期:2014-05-17  浏览次数:20656 次

新手在练习用ajax的时候出现这样的问题
HTML code

function change(v1){
              var xmlhttp = getXmlHttpRequest();
              xmlhttp.open('post','city.do',true);
              xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
              xmlhttp.onreadystatechange=function(){
                  if(xmlhttp.readyState == 4){
                      var txt = xmlhttp.responseText;
                      var arr = txt.split(';');
                      $('s2').innerHTML = '';
                      for(i=0;i<arr.length;i++){
                          var arr1 = arr[i];
                          var arr2 = arr1.split(',');
                          var op = new Option(arr2[1],arr2[0]);
                          $('s2').options[i] = op;
                      }
                  }
              };
              xmlhttp.send('name = ' +v1);
          }
      </script>
  </head>
  
  <body style="font-size:30px;">
      <select id="s1" style="width:120px;" onchange="change(this.value);">
          <option value="hn">湖南</option>
          <option value="bj">北京</option>
      </select>
      <select id="s2" style="width:120px;"></select>
  </body>


这是jsp中的内容

Java code

public class Action extends HttpServlet{
    public void service(HttpServletRequest request, HttpServletResponse response)
    throws ServletException,IOException{
        String uri = request.getRequestURI();
        String path = uri.substring(uri.lastIndexOf("/"),uri.lastIndexOf("."));
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        if(path.equals("/city")){
            String name = request.getParameter("name");
            if(name.equals("hn")){
                out.println("yy,岳阳;hh,怀化;cs,长沙");
            }else if(name.equals("bj")){
                out.println("cy,朝阳;hd,海淀;dc,东城;xc,西城");
            }
        }
        out.close();
    }
}


这是java中的action,然后就配置了web
XML code

<welcome-file-list>
    <welcome-file>city.jsp</welcome-file>
  </welcome-file-list>
  
   <servlet>
      <servlet-name>Action</servlet-name>
      <servlet-class>web.Action</servlet-class>
  </servlet>
  
  <servlet-mapping>
      <servlet-name>Action</servlet-name>
      <url-pattern>*.do</url-pattern>
  </servlet-mapping>



目的是当我选择北京的时候,可以出来北京的那几个地名,选择湖南的时候可以出现湖南的那几个地名,但是出现了异常

------解决方案--------------------
好像问错地方了...