惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

酷 壳 – CoolShell
酷 壳 – CoolShell
H
Hacker News: Front Page
P
Palo Alto Networks Blog
T
ThreatConnect
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
T
True Tiger Recordings
P
Privacy & Cybersecurity Law Blog
B
Blog
IT之家
IT之家
Last Week in AI
Last Week in AI
F
Full Disclosure
Hacker News: Ask HN
Hacker News: Ask HN
C
Comments on: Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Cybersecurity and Infrastructure Security Agency CISA
Microsoft Security Blog
Microsoft Security Blog
博客园 - 【当耐特】
N
News and Events Feed by Topic
NISL@THU
NISL@THU
腾讯CDC
雷峰网
雷峰网
Security Latest
Security Latest
李成银的技术随笔
M
Microsoft Research Blog - Microsoft Research
L
LangChain Blog
L
Lohrmann on Cybersecurity
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Check Point Blog
Y
Y Combinator Blog
Recent Announcements
Recent Announcements
博客园 - Franky
N
News | PayPal Newsroom
V
V2EX
A
About on SuperTechFans
The Register - Security
The Register - Security
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google Online Security Blog
Google Online Security Blog
MyScale Blog
MyScale Blog
Cisco Talos Blog
Cisco Talos Blog
Vercel News
Vercel News
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
The Hacker News
The Hacker News
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
IntelliJ IDEA : IntelliJ IDEA – the Leading IDE for Professional Development in Java and Kotlin | The JetBrains Blog
爱范儿
爱范儿
A
Arctic Wolf
L
LINUX DO - 最新话题
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

博客园 - 右手边

C#动态执行代码 这是一个广为流传的关于项目管理的通俗讲解 [史上最狂妄的演讲]你不服都不行<英汉版>~~ - 右手边 - 博客园 关于页面动态添加控件 利用反射动态创建对象 使用反射技术动态创建类对象(实例代码) 利用反射来动态创建实例和调用方法(上) 基于ASP.NET的Web动态控件创建 家中请客(最佳ERP教材)- - 2007年4月25日 经典的几句英文 网上英语免费杂志在钱订阅网址 将(Oracle)数据库表导出到Excel,并生成文件(C#实现) B/S结构与C/S结构 Ajax简单例子 Oracle 启动与关闭例程 AJAX的原理 VB资料库-无限下载 ASP.Net中程序构架与程序代码的分离 Oracle 系统结构
AJAX三步走
右手边 · 2007-05-22 · via 博客园 - 右手边

ajax 简单 例子,共三步。

例子涉及到本地的数据库,故不能在你本地正常运行,但思路是对的

第一步:(通用) 把下面的代码写入一个ajax.js文件中,send_request()方法有5个参数,
1,提交的方法;
2,url,也可以通过它传递参数;
3,只是参数;
4,返回类型,text或xml;
5,回调函数的方法名称。
var http_request = false;

//send
function send_request(method,url,content,responseType,callback){
 http_request = false;
 if(window.XMLHttpRequest){
  http_request = new XMLHttpRequest();
  if(http_request.overrideMimeType){
   http_request.overrideMimeType("text/xml");   
  }
 }else if(window.ActiveXObject){
  try{
   http_request = new ActiveXObject("Msxm12.XMLHTTP");
  }catch (e){
   try{
    http_request = new ActiveXObject("Microsoft.XMLHTTP");
   }catch (e){

       }
  }
 }
 if(!http_request){
  window.alert("E XMLHttpReqeust");
  return false;
 }
 if(responseType.toLowerCase() == "text"){
  //http_request.onreadystatechange = processTextResponse;
  http_request.onreadystatechange = callback;
 }else if(resposeType.toLowerCase() == "xml"){
  //http_request.onreadystatechange == processTextResponse;
  http_request.onreadystatechange = callback;
 }else{
  window.alert("E1");
  return false; 
 }

  if(method.toLowerCase() == "get"){
  http_request.open("GET",url,true);
 }else if(method.toLowerCase() == "post"){
  http_request.open("POST",url,true);
  http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
 }else{
  window.alert("E2");
 }
 http_request.send(content);
}

//Text
function processTextResponse(){
 if(http_request.readyState == 4){
  if(http_request.status == 200){
   document.form1.rs.value = http_request.responseText;
  }else{
   alert("E3");
  }
 }
}

//XMl
function processXMLRequest(){
 if(http_request.readyState == 4){
  if(http_request.status == 200){
   document.form1.rs.value = http_request.responseText;
  }else{
   alert("E4");
  }
 }
}

第二步:把此脚本放入页面(通用)

<script language="javascript">
 //取组下的3个clip
 function update(type,id) {
  var f = document.p;
  if("clip" == type){
    one.innerHTML = "  正在处理请求的内容 <img src='/images/icon/loading.gif'></img>";
    send_request("GET","/common2/query.jsp?groupbyclip_id="+id,null,"text",processTextResponse_one);
  }
  var f = document.p;
  if("cpid" == type){
    one.innerHTML = "  正在处理请求的内容 <img src='/images/icon/loading.gif'></img>";
    send_request("GET","/common2/query.jsp?cp_id="+id,null,"text",processTextResponse_one);
  }
 }

</script>

第三步:创建jsp处理页面,接受参数,处理,out.print()输出就可以了,回调函数会接收到打印出的内容(根据业务需求作相应的修改<%
 //根据省id获取城市列表
 if(request.getParameter("province_id") != null &&request.getParameter("province_id") != "-1"){
  ArrayList provinceList = new ArrayList();
  try{
   provinceList = Factory.db_city.queryByProvinceId(Integer.parseInt(request.getParameter("province_id")));
  }catch(Exception e){
   out.print("根据省查找city出错");
  }
  if(provinceList != null){
   out.println("<select name='city' id='city' onchange=\"update('city')\">");
   out.println("<option value='-1'>请选择城市</option>");
   for(int i=0;i<provinceList.size();i++){
    CityBean cityBean = (CityBean)provinceList.get(i);
    out.println("<option value="+cityBean.getCity_id()+">"+cityBean.getCity()+"</option>");           
   }
   out.print("</select>");
  }
 }

  //根据city_id查询区县
 if(request.getParameter("city_id") != null &&request.getParameter("city_id") != "-1"){
  ArrayList districtList = new ArrayList();
  try{
   districtList = Factory.db_district.queryByCityId(Integer.parseInt(request.getParameter("city_id")));
  }catch(Exception e){
   out.print("根据省查找district出错");
  }
  if(districtList != null){
   out.println("<select name='district' id='district'>");
   out.println("<option value='-1'>请选择区县</option>");
   for(int i=0;i<districtList.size();i++){
    DistrictBean districtBean = (DistrictBean)districtList.get(i);
    out.println("<option value="+districtBean.getDistrict_id()+">"+districtBean.getDistrict()+"</option>");           
   }
   out.print("</select>");
  }
 }
 %>