六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 56|回复: 0

AJAx例子(get请求方式)

[复制链接]

升级  48%

34

主题

34

主题

34

主题

秀才

Rank: 2

积分
122
 楼主| 发表于 2013-2-6 08:41:51 | 显示全部楼层 |阅读模式
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><base href="<%=basePath%>"><title>My JSP 'index.jsp' starting page</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"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><script type="text/javascript">var xmlHttpReq;//创建一个XmlHttpRequest对象function createXmlHttpRequest() {if (window.XMLHttpRequest) {xmlHttpReq = new XMLHttpRequest();//非IE浏览器} else if (window.ActiveXObject) {xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");//IE浏览器}}function check(){var name  = document.getElementById("tx1").value;if(name!=""){alert(name);//1.创建一个XmlHttpRequest对象createXmlHttpRequest();//2.调用XMLHTTPRequest对象的 open方法(),//初始化XMLHttpRequest组件//处理缓存问题 url后面再加个时间参数,保证每次请求的url都不同var url = "validate?name="+name+"&date="+new Date().getTime();alert("url-1"+url);url = encodeURI(url);alert("url-1"+url);url = encodeURI(url);alert("url-1"+url);xmlHttpReq.open("GET",url,true);// "Get"是请求方式,//url向后台服务器发送请求的url//true 代表使用异步请求, 可选参数,默认为true //3.注册回调函数xmlHttpReq.onreadystatechange=callBack;//callBack 为自定义的回调函数的名字 注意:后面没有括号//当xmlHttpReq对象的readystate状态改变时自动触发 回调函数callBack//4.把请求发送到服务器xmlHttpReq.send(); //如果是get请求send方法不需要加参数}else{alert("请输入数据");}}function callBack(){//alert("readyState:"+xmlHttpReq.readyState);if(xmlHttpReq.readyState==4){//ajax引擎初始化成功if(xmlHttpReq.status==200){//与tomcat(服务器)交互成功,http协议成功//alert("xmlHttpReq.status:"+xmlHttpReq.status);var text = xmlHttpReq.responseText;//通过responseText 属性,取出服务器端返回的数据alert(text);var spanObj = document.getElementById("sp1");spanObj.innerHTML="<font color='red'>" +text+"</font>"; //把值显示到span中}}}</script></head><body><p align="center">用户注册</p><table align="center"><tr><td>用户名</td><td><input type="text" id="tx1"><input type="button"  value="检测"></td><td><span id="sp1"></span></td></tr></table></body></html> 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表