js实现ajax
来源:互联网 发布:百合 知乎 编辑:程序博客网 时间:2024/06/16 22:18
//jsp页面中
1:创建XMLHttpRequest对象
if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
} else{
xmlHttpRequest = new ActiveXObject("Microsoft.ActiveXObject");
}
2:打开URL
xmlHttpRequest.open("GET", url, 是否异步); //true:异步 false:同步
3:设置头信息
xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
4:设置回调函数
xmlHttpRequest.onreadystatechange = function(evt){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
//获得相应信息
xmlHttpRequest.responseText 或者 xmlHttpRequest.responseXML
//根据相应文本进行处理
//JavaScript中字符串转JSON对象
eval("("+字符串+")");
}
}
5:发送请求
xmlHttpRequest.send();
//服务提供者 -- 服务器(Servlet)
doPost(HttpServletRequest request, HttpServletResponse response){
request.getParameter("请求参数");
处理步骤.....
//如果有响应信息 获得响应输出流
PrintWriter out = response.getWriter();
out.print("响应文本");
out.flush();
out.close();
}
1:创建XMLHttpRequest对象
if(window.XMLHttpRequest){
xmlHttpRequest = new XMLHttpRequest();
} else{
xmlHttpRequest = new ActiveXObject("Microsoft.ActiveXObject");
}
2:打开URL
xmlHttpRequest.open("GET", url, 是否异步); //true:异步 false:同步
3:设置头信息
xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
4:设置回调函数
xmlHttpRequest.onreadystatechange = function(evt){
if(xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200){
//获得相应信息
xmlHttpRequest.responseText 或者 xmlHttpRequest.responseXML
//根据相应文本进行处理
//JavaScript中字符串转JSON对象
eval("("+字符串+")");
}
}
5:发送请求
xmlHttpRequest.send();
//服务提供者 -- 服务器(Servlet)
doPost(HttpServletRequest request, HttpServletResponse response){
request.getParameter("请求参数");
处理步骤.....
//如果有响应信息 获得响应输出流
PrintWriter out = response.getWriter();
out.print("响应文本");
out.flush();
out.close();
}
0 0
- 【Js】js实现ajax
- AJAX-prototype.js实现Ajax
- ajax js实现
- js实现ajax
- js实现ajax
- js实现ajax请求
- ajax+JS 实现分页
- Ajax-Js实现
- 原生js实现Ajax
- 原生js实现ajax
- 原生JS实现Ajax
- js实现ajax
- 原生js实现ajax
- AJAX实现(JS)
- 用js实现Ajax
- Js实现Ajax
- js实现ajax
- js实现ajax
- Linux下cmake简易教程
- Python - 生成Robot Framework 类
- 计算机组成原理(1)
- Activity的四种启动模式及其应用
- java类之间继承、实现、组合、聚合、关联、依赖
- js实现ajax
- 使用easyBCD引导启动ubuntu14.04
- C# iOS Xamarin tableview
- MYSQL导入导出.sql文件
- Python--网络爬虫之基础篇
- Spiral Matrix
- TextView显示Gif图片实现图文混排
- 最小费用流
- 智猪博弈论