Ajax中利用jQuery简单实现异步交互
来源:互联网 发布:淘宝充值劲舞团未到账 编辑:程序博客网 时间:2024/05/17 07:46
function verify(){//alert("被点击了。。。。。。。");//得到id为loginName的标签对象var jqObject = $("#loginName");//得到文本框中的值var name = jqObject.val();//用JQ中的$.get()方法去访问服务器/*JQ中的get方法接受的参数 * get(url, [data], [callback], [type]) * url 待载入页面的URL地址 * data (可选) 待发送 Key/value 参数。 * callback (可选) 载入成功时回调函数。 * type (可选) 返回内容格式,xml, html, script, json, text, _default。 */$.get("AjaxTestServlet?loginName=" + name,null,callback);}//从服务器返回来的数据封装在callback函数的参数中function callback(data){alert(data);}
主页html为如下
<!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=GBK"><title>Insert title here</title><script type="text/javascript" src="jquery-1.7.1.js"></script><script type="text/javascript" src="verify.js"></script></head><body><form name="form1" action="AjaxTestServlet" method="POST">请输入注册用户名: <input name="loginName" type="text" id="loginName"><input type="button" name="checkLoginName" value="有效性检查"onclick="verify()"><br></form></body></html>
接受的响应的服务器端的程序为AjaxTestServlet
import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class AjaxTestServlet */@WebServlet("/AjaxTestServlet")public class AjaxTestServlet extends HttpServlet {private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public AjaxTestServlet() { super(); // TODO Auto-generated constructor stub }/** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubdoPost(request,response);}/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// post按doGet()方法处理response.setContentType("text/html");PrintWriter out = response.getWriter();//这里假设已经注册过了四位用户String[] logined = {"admin","users","dywdyw","zhaom_916"};//接收从客户端提交的loginName参数String loginName = request.getParameter("loginName");//创建一个存放响应内容的字符串String responseContext = "true";for (int i=0;i<logined.length;i++){//遍历已注册用户列表,如果发现提交的注册名已存在,则修改响应内容if (loginName.equals(logined[i]))responseContext = "false";}//将处理结果返回给客户端out.println(responseContext);out.flush();out.close();}}中间遇到过一个小问题 就是要求js与html的编码一致 不然会出现乱码
- Ajax中利用jQuery简单实现异步交互
- jquery ajax异步交互
- 利用JQuery实现简单AJAX
- 利用JQuery实现ajax异步刷新页面
- ajax实现异步交互
- Ajax实现异步交互
- ajax+jquery实现简单的数据交互
- 利用jquery框架中ajax实现前台与struts2中action交互
- jquery+ajax+ashx Ajax异步数据交互
- jQuery实现Ajax异步
- 利用JQuery+Ajax实现aspx页面无刷新异步请求
- django 1.4 利用jquery实现ajax ‘get|post’异步请求
- 利用 Linq+Jquery+Ajax 实现异步分页功能
- 利用 Linq+Jquery+Ajax 实现异步分页功能
- 使用Jquery实现异步交互
- JQuery和AJAx的异步交互
- Jquery+ajax+php实现简单的异步提交
- php+JQuery+Ajax简单实现页面异步刷新
- poj 1038 Bugs Integrated, Inc.
- 一个月培养一个好习惯 个人提升
- 黑马程序员 Params可变参数
- 修改PHP文件上传限制
- android学习记录
- Ajax中利用jQuery简单实现异步交互
- 黑马程序员 类型化DataSet更新
- Myeclipse 7.5+SQL 2005+Tomcat 6连接池配置
- 黑马程序员 将Access数据库里的数据导入Sql server数据库里
- uva10603
- copyProperties的用法
- 寄售及其流程
- 2
- MyEclipse 6.5破解码生成器