ajax实际的应用

假设两个选择框,前面一个是省份,后面一个是地级市。当选择某一省份的时候,后面的选择框会有此省份对应的地级市。可以根据ajax来实现。

function load{

if(true){
      var baseURL = "/prpall/processPrpDBusiLevel.do?actionType=getCityInfo";
      var url = baseURL + "&busiUpperCode=" + trim(field1.value)+"&busiLevel="+level;
      xmlRequest = createXMLHttpRequest1();
      xmlRequest.onreadystatechange = processCityCodeStateChange;
      try {
          xmlRequest.open("POST",url,false);
      }catch (e) {
          alert(e);
      }
      xmlRequest.send(null);

}

}

function processCityCodeStateChange(){
  if (xmlRequest.readyState == 4) { // Complete
      if (xmlRequest.status == 200) { // OK response
          var retValue = trim(xmlRequest.responseText);
          if(retValue!=""){
            if(retValue.indexOf("error:")==0){
            }else{         
              var optionArray  =  retValue.split(GROUP_SEPARATOR);
              var value;  
              var text; 
              field2.add(new Option("请选择","",true,true));
              var order = 0;
              for(var i=0;i<optionArray.length;i++){
                var optionText = optionArray[i].split(FIELD_SEPARATOR);
                value = optionText[0];  
                text = optionText[1];
                field2.add(new Option(text,value));
              }
            }
            field2.add(new Option("其他城市","999999"));
          }
         
      } else {
          alert("Problem: " + xmlRequest.statusText);
      }
  }
}

function createXMLHttpRequest1() {
    try {
    // Attempt to create it "the Mozilla way"
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        }
    // Guess not - now the IE way
        if (window.ActiveXObject) {
            return new ActiveXObject(getXMLPrefix() + ".XmlHttp");
        }
    }
    catch (ex) {
    }
    return false;
}

服务器端(jsp):

<%
UIRateFromInfoAction uiRateFromInfoAction= new UIRateFromInfoAction();
OutputStream outputStream= response.getOutputStream();
String provinceCode= request.getParameter("provinceCode");
String cityCode= request.getParameter("cityCode");
String districtCode= request.getParameter("districtCode");
String constructCode= request.getParameter("constructCode");
double rate = uiRateFromInfoAction.getRate(provinceCode, cityCode,districtCode, constructCode);
BigDecimal bRate= new BigDecimal(rate);
bRate = bRate.setScale(2, BigDecimal.ROUND_HALF_UP);
String rateString= bRate.toString();

try {
 outputStream.write(rateString.getBytes());
 outputStream.flush();
} catch (Exception e) {
 throw e;
}finally{
 outputStream.close();
}

%>

上一篇:WebBrowser设置Cookie


下一篇:DataInputStream FileInputStream 区别