论坛首页 Web前端技术论坛

JQuery ajax Demo

浏览 27372 次
精华帖 (0) :: 良好帖 (2) :: 新手帖 (1) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-09-06  
网上的 Jquery ajax Demo 大多都是基于php
很少 有java的 今天就把自己的Demo贴出来 和大家共同学习
现在就  Jquery ajax 的 $.ajax(),$.post(),$.get();

首先是  服务端的Servlet 演示这三个函数的用法对都是用的同一个 服务端
package com.june.servlet;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;

public class jqueryAjaxServer extends HttpServlet {
     public jqueryAjaxServer(){
    	 super();
     }
     public void doGet(HttpServletRequest request,HttpServletResponse response)
     throws IOException ,ServletException {
    	 response.setContentType("text/html;charset=utf-8");
    	 PrintWriter out=response.getWriter();
    	 String account=request.getParameter("account");
    	 if("iamcrzay".equals(account)){
    		 out.print("Sorry,the user is exist");
    	 }
    	 else{
    		 out.print("Congratulation,this accont you can use!!!!");
    	 }
    	 out.close();
     }
     public void doPost(HttpServletRequest request,HttpServletResponse response)
     throws IOException ,ServletException {
    	 this.doGet(request, response);
     }
}



下面是WEB.XML
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<servlet>
	   <servlet-name>jqueryAjaxServer</servlet-name>
	   <servlet-class>com.june.servlet.jqueryAjaxServer</servlet-class>
	</servlet>
        <servlet-mapping>
	   <servlet-name>jqueryAjaxServer</servlet-name>
	   <url-pattern>/jqueryAjax</url-pattern>
	</servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>



下面是Jsp页面
第一个是 jqueryAjax.jsp  本页使用的是$.ajax()
<%@ page language="java"  pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>jquery ajax</title> 
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
	<script language="javascript">
	     $(function(){
	           $('.sumbit').click(function(){
	           if($('#account').val().length==0){
	               $('.hint').text("用户名不能位空").css({"background-color":"green"}); 
	           }
	           else{
	           $.ajax({
	             url:'jqueryAjax',
	             data:{account:$('#account').val()},
	             error:function(){
	             alert("error occured!!!");
	             },
	             success:function(data){
	              $('body').append("<div>"+data+"</div>").css("color","red");
	  
	             }
	             
	           });}
	           });
	           });
           	     
	   
	       
	</script>
  </head>
  
  <body>
                <h3 align="center">jquery AjaX</h3>
                <hr>
                <label>请输入用户名 :</label>
                <input id="account" name="account" type="text">
                <input class="sumbit" type="button" value="检测">
                <div class="hint">
                </div>
  </body>
</html>



第二个用的是  $.post()

<%@ page language="java"  pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>  
    <title>jquery ajax</title> 
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
	<script language="javascript">
	     $(function(){
	         $('.sumbit').click(
	          function(){
	            if($('#account').val().length==0){
	                $('.hint').text("The account is cant't be null").css({"color":"red","background-color":"yellow"});
	            }
	            else{
	            $.post("jqueryAjax","account="+$('#account').val(),function(data){
	               $('.hint').text(data).css({"color":"red","background-color":"yellow"});
	            })
	            }
	         });
	     });	       
	</script>
  </head>
  
  <body>
                <h3 align="center">jquery Ajax</h3>
                <hr>
                <label>请输入用户名 :</label>
                <input id="account" name="account" type="text">
                <input class="sumbit" type="button" value="检测">
                <div class="hint">
                </div>
  </body>
</html>



第三个是用的$.get()

<%@ page  pageEncoding="utf-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>   
    <title>jquery get</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<script src="js/jquery-1.2.6.js" type="text/javascript"></script>
	<script type="text/javascript">
	     $(function(){
	            $('.sumbit').click(function(){
	                  if($('#account').val().length==0){
	                     $('.hint').html("用户名不能位空!!!").css({"color":"#ffoo11","background":"blue"});
	                  }
	                  else{
	                      $.get("jqueryAjax","account="+$('#account').val(),
	                           function(data){
	                            $('.hint').html(data).css({"color":"#ffoo11","background":"green"});
	                      });
	                  }
	            });
	     });
	</script>

  </head>
  
  <body>
        <h3 align="center">jquery AjaX</h3>
                <hr>
                <label>请输入用户名 :</label>
                <input id="account" name="account" type="text">
                <input class="sumbit" type="button" value="检测">
                <div class="hint">
                </div>
  </body>
</html>


欢迎大家拍砖  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
   发表时间:2008-09-07  
最近刚用到jquery框架,非常感谢!
0 请登录后投票
   发表时间:2008-09-08  
我说大哥,咋不能运行呢?有问题呀
0 请登录后投票
   发表时间:2008-09-08  
不会吧   我在ff 和ie 6 上都通过了的 ``````````
楼上的哥们你把你的错误信息 贴出来看看
0 请登录后投票
   发表时间:2008-09-09  
本页使用的是$.ajax()
==============
当文本框里为空时,按大哥的意思是"用户名不能位空"能显示出来,但页面上报错JS错误,什么"childNodes"为空不是对象",咋一个回事呀? 
0 请登录后投票
   发表时间:2008-09-10  
查了不少,就是没发现有和struts的,比如
List names=....................得到是一个集合;
request.setAttribute("names",names);
那可以直接像上面的
success:function(data){  
                  $('body').append("<div>"+data+"</div>").css("color","red");   }  

好像JS不能这样处理,那就要换个XML。JSON来存储,不知可否提供些例子,
0 请登录后投票
   发表时间:2008-09-10  
JSON提供一个类处理的,可以直接把一个LIST转换成JSON格式叫jsonarray,自己去下载,在客户端用.getJSON就可以直接得到了啊。呵呵
0 请登录后投票
   发表时间:2008-09-10  
已经略有所闻,JSONarray JSONObject,因为初学,所以希望有个例子参考下,
牛人可以的话帮助下初学的。
PrintWriter out = response.getWriter();
   JSONObject obj = new JSONObject();
JSONArray js = new JSONArray();
   //这里的数据拼装一般是从数据库查询来的
    for(int i=0;i<3;i++)
    {
     JSONObject objtemp = new JSONObject();
     objtemp.put("id", i);
     objtemp.put("age", "23");
     objtemp.put("name", "test"+i);
     objtemp.put("address", "test");
     js.add(objtemp);
    }
   obj.put("js",js);
         out.print(obj.toString());
  }catch(Exception e)
  {
   e.printStackTrace();
   System.out.print("消费明细json存储异常");
  }
  return null;
}
难道都要这样么?定义一个PrintWriter ,不用request.setAttribute()了
0 请登录后投票
   发表时间:2008-09-11  
晕哦

jsonArray jsonList=jsonarry.fromObject(list);好象是这个,返回的就是JSON格式的LIST


res.getWrite().print(jsonList);客户端就收到啦

$.getJSON('a.action',function(data){//这里的data就是上面就是上面返回的东西
   alert(data.length);//显示有多少条记录
   alert(data[0].id);//这个id拿来的呢,就是HIBERNATE的那个是跟数据库对应的类哦,里面有个id字段
});
0 请登录后投票
   发表时间:2008-10-07  
$.post 如何使用struts 的地址报JS的错 谁解答一下
0 请登录后投票
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics