Ajax与Json交互实例
来源:互联网 发布:会计网络继续教育考试 编辑:程序博客网 时间:2024/06/07 16:29
1.用户验证:
前端将用户名传递给后台,后台通过查找如果法相用户名存在则打包json为{exist:true}不存在为{“exist”:false}
具体实现:
后台:
public void getJsonObj(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
String userName=request.getParameter("userName");
JSONObject jsonObject=new JSONObject();
if("xd".equals(userName)){
jsonObject.put("exist", true);
}else{
jsonObject.put("exist", false);
}
out.println(jsonObject);
out.flush();
out.close();
}
前端:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function checkUserName(){
var xmlHttp;
var userName=document.getElementById("userName").value;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
var dataObj=eval("("+xmlHttp.responseText+")");
if(dataObj.exist){
document.getElementById("tip").innerHTML="<img src='no.png'/> 该用户名已经存在";
}else{
document.getElementById("tip").innerHTML="<img src='ok.png'/> 该用户名可用";
}
}
};
xmlHttp.open("get", "getAjaxName?action=jsonObject&userName="+userName, true);
xmlHttp.send();
// xmlHttp.open("post", "getAjaxName", true);
//xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
//xmlHttp.send("name=jack&age=11");
}
</script>
</head>
<body>
<div style="text-align: center;">
<table align="center" id="register" border="1px">
<tr>
<th>用户注册</th>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" id="userName" name="userName" onblur="checkUserName()"/> <font id="tip"></font></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="text" id="password" name="password"/></td>
</tr>
<tr>
<td>确认密码:</td>
<td><input type="text" id="password2" name="password2"/></td>
</tr>
<tr>
<td><input type="submit" value="注册"/></td>
<td><input type="button" value="重置"/></td>
</tr>
</table>
</div>
</body>
</html>
2.实现二级联动:
这个实际上是完成选择了省可以直接加载相应的市:
具体代码:
后台:
private void ejld(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out=response.getWriter();
String shengId=request.getParameter("shengId");
JSONObject resultJson=new JSONObject();
JSONArray jsonArray=new JSONArray();
JSONObject temp=null;
switch(Integer.parseInt(shengId)){
case 1:{
temp=new JSONObject();temp.put("id", 1);temp.put("text", "ÄϾ©");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 2);temp.put("text", "ÄÏͨ");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 3);temp.put("text", "Ì©ÐË");jsonArray.add(temp);
break;
}
case 2:{
temp=new JSONObject();temp.put("id", 4);temp.put("text", "¼ÃÄÏ");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 5);temp.put("text", "ÑĮ̀");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 6);temp.put("text", "ÅîÀ³");jsonArray.add(temp);
break;
}
case 3:{
temp=new JSONObject();temp.put("id", 7);temp.put("text", "º¼ÖÝ");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 8);temp.put("text", "Äþ²¨");jsonArray.add(temp);
temp=new JSONObject();temp.put("id", 9);temp.put("text", "ÎÂÖÝ");jsonArray.add(temp);
break;
}
}
resultJson.put("rows", jsonArray);
out.println(resultJson);
out.flush();
out.close();
}
前端:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function loadInfo(){
var shengId=document.getElementById("sheng").value;
shi.options.length=0; // 删除所有市下拉框的选项
var xmlHttp;
if(window.XMLHttpRequest){
xmlHttp=new XMLHttpRequest();
}else{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState==4 && xmlHttp.status==200){
alert(xmlHttp.responseText);
var dataObj=eval("("+xmlHttp.responseText+")");
for(var i=0;i<dataObj.rows.length;i++){
var o=dataObj.rows[i];
shi.options.add(new Option(o.text,o.id));
}
}
};
xmlHttp.open("get", "getAjaxInfo?action=ejld&shengId="+shengId, true);
xmlHttp.send();
}
</script>
</head>
<body>
省:
<select id="sheng" onchange="loadInfo()">
<option value="1">江苏省</option>
<option value="2">山东省</option>
<option value="3">浙江省</option>
</select>
市
<select id="shi">
</select>
</body>
</html>
- Ajax与Json交互实例
- Ajax&Json交互实例
- json与ajax对服务器的交互
- Ajax与servlet交互的实例
- Ajax与servlet交互的实例【转】
- appcan与.net的Ajax交互实例
- Spring MVC与Ajax交互实例
- Ajax与servlet交互的实例
- ajax与servlet交互实例,初学者看
- Ajax与后台交互小实例
- Ajax Wcf 交互实例
- 用jquery +ajax 实现与php后台交互json数据
- struts2中使用json格式ajax与后端action交互
- 通过AJAX与Servlet进行交互:通过JSON传递数据
- SpringMVC与Ajax(Json,String,Form)数据交互
- JQuery处理json与ajax返回JSON实例
- JQuery处理json与ajax返回JSON实例
- JQuery处理json与ajax返回JSON实例
- 如何为电脑创建新的硬盘分区
- matlab gaot工具箱安装详细说明
- Spring MVC controller间跳转 重定向 传参
- pandas小记:pandas时间序列分析和处理Timeseries
- windows 配置mysql
- Ajax与Json交互实例
- Preference 三种监听事件说明
- python Image图形处理
- 8.4 List和Set
- ANSI、ASCII、Unicode和UTF-8编码
- mvc ajax
- 配置hibernate时出错原因及解决方案
- JavaScript原型中的哲学思想
- 最后一公里极速配送(一)