<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ajax</title> <script language="javascript">
//<script language="javascript" type="application/javascript"> // 如果上方的代码不能够完整实现就使用这个 //IE浏览器实例化方式
//var h = new ActiveXObject("Msxml2.XMLHTTP"); //var p = new ActiveXObject("Microsoft.XMLHTTP"); //非IE浏览器
//var f = new XMLHttpRequest(); //alert(p.getAllResponseHeaders()); var hx = false;
//定义函数
function test(user_id)
{
//所有浏览器通用实例化代码
if(window.XMLHttpRequest) //非IE IE7版本及以上 都支持非ie形式
{
hx = new XMLHttpRequest(); //如果是非IE浏览器 那么就会实例化
// alert("qqq"); //如果是实例化成功上方的,那么就会输出这句话
}
else if(window.ActiveXObject) //IE
{
try{
hx = new ActiveXObject("Msxml2.XMLHTTP"); //实例化
// alert("qqq2"); //如果实例化成功上方的 那么就会输出这句话
}
catch(e)
{
alert(e);
try
{
hx = new ActiveXObject("Microsoft.XMLHTTP"); //实例化
// alert("qqq3"); //如果实例化成功上方的 那么就会输出这句话
}
catch(e)
{
alert(e);
}
}
} //测试创建XMLHttpRequest是否成功
if(!hx)
{
alert("创建XMLHttpRequest失败");
}
else
{
alert("创建XMLHttpRequest成功");
} //异步请求的请求类型有两种方式 一种是 get 另一种是 post 两种方法都有各自的方式 //get方式 alert(new Date().getTime());
// 1 设置异步请求的url等信息
hx.open("GET","ajaxtest?userid=" + user_id + "&nocache = " + new Date().getTime() , true); //("请求类型 GET/POST",URL,是否异步 true/false)
// 2 设置回调函数 事件处理器
hx.onreadystatechange = function() //匿名函数
{
getResult("1");
}
// hx.onreadystatechange = getResult; //将回调函数的函数名作为一个事件处理器给 hx.onreadystatechange
//调用请求的发送
hx.send(null); //在需要请求传送参数时设置异步请求时用 post 方式 } //回调函数
function getResult(type)
// function getResult()
{
//
//alert("readyState = "+hx.readyState);
if(hx.readyState == 4) //判断是否完成
{
if(hx.status == 200) //判断服务器是否完成,正常
{
// alert("回调信息 = " + hx.responseText); //返回的值
alert(hx.responseText); alert(type); // alert("Headers = " + hx.getAllResponseHeaders()); //获取返回函数的全部头信息 // alert("Server = " + hx.getResponseHeader("Server")); //获取部分头信息--回调信息
}
else
{
alert("错误状态码 = " + hx.status + "状态文本信息 = " + hx.statusText);
}
}
} /* //post方式 // 1 设置异步请求的url等信息
hx.open("POST","ajaxtest",true); //("请求类型 GET/POST",URL,是否异步 true/false)
// 2 设置回调函数 事件处理器 hx.onreadystatechange = function() //匿名函数
{
getResult("1");
} // hx.onreadystatechange = getResult; //将回调函数的函数名作为一个事件处理器给 hx.onreadystatechange //调用请求的发送
//在需要请求传送参数时设置异步请求时用 post 方式
hx.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
//post方式 数据传输 get方式可以直接放置于URL中
hx.send("userid=" + user_id); }
//回调函数
function getResult(type)
{
//
//alert("readyState = "+hx.readyState);
if(hx.readyState == 4) //判断是否完成
{
if(hx.status == 200) //判断服务器是否完成,正常
{
// alert("回调信息 = " + hx.responseText); //返回的值
alert(hx.responseText);
// alert("Headers = " + hx.getAllResponseHeaders()); //获取返回函数的全部头信息 // alert("Server = " + hx.getResponseHeader("Server")); //获取部分头信息--回调信息
}
else
{
alert("错误状态码 = " + hx.status + "状态文本信息 = " + hx.statusText);
}
}
}
*/
//alert("Server = " + hx.getAllResponseHeaders("Server")); </script> </head> <body> 用户代码:<input type="text" onchange="test(this.value);" /> </body>
</html>

Ajax

package com.hanqi;

import java.io.IOException;
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 ajaxtest
*/
@WebServlet("/ajaxtest")
public class ajaxtest extends HttpServlet {
private static final long serialVersionUID = 1L; /**
* Default constructor.
*/
public ajaxtest() {
// TODO Auto-generated constructor stub
} /**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8"); response.setContentType("text/html; charset=utf-8"); String userid = request.getParameter("userid"); if(userid != null && userid.trim().length() > 0)
{
//查找用户信息
if(userid.equals("abc"))
{
response.getWriter().append("用户代码已存在!"); }
else
{
response.getWriter().append("用户代码不存在,可以使用!");
} }
else
{
response.getWriter().append("请正确填写!");
} response.getWriter().append("test ajax"); response.getWriter().append("Served at: ").append(request.getContextPath());
} /**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
} }

AjaxTest

20151217--Ajax的一点补充的更多相关文章

  1. 对于Tomcat服务器环境变量和启动配置的一点补充

    我们之前第一次使用Tomcat服务器运行jsp应用时,曾经给Tomcat配置过一个环境变量CATALINA_HOME,这个变量指定了Tomcat的安装位置,对于多个开发项目,我们一般会释放多个tomc ...

  2. WCF的一点补充-Restful相关

    参考 配置WCF心得 对REST架构 风格下WCF的一点补充 Securing WCF REST Service with Azure AppFabric Access Control Service ...

  3. Ajax技术使用补充

    Ajax技术使用补充 一.Ajax发送数据的几种形式 发送字符串或数字 $.ajax({ url:"/ajax_test.html/", type:'POST', data:{'v ...

  4. C++基础--引用的一点补充

    这一篇是对引用的一点补充,内容基本上是来自<C++ primer plus>一书第八章的内容. 前面一篇介绍了引用的一点特点,这里补充一个,将引用用于类对象的时候,有一个体现继承的特征,就 ...

  5. 关于asp.net与jquery ajax 的一些补充

    补充1:asp.net 与后台交互除了用之前写得$.ajsx()外 还可以直接使用$.get()  , $.post()等. 补充2:jquery 跨域请求 例如: JSONP 跨域: $.ajax( ...

  6. Android 12(S) 图形显示系统 - Surface 一点补充知识(十二)

    必读: Android 12(S) 图形显示系统 - 开篇 一.前言 因为个人工作主要是Android多媒体播放的内容,在工作中查看源码或设计程序经常会遇到调用API: static inline i ...

  7. 关于bundle install 的一点补充

    在第一次运行bundle install之后,生成了Gemfile.lock文件,里面记录gem的具体版本号,按照官方文档说明,以后运行bundle install就不会再依据Gemfile,而是根据 ...

  8. 【Linux】 linux的进程系统一点补充

    linux进程系统 ■ 程序 vs. 进程 程序静态地存放在磁盘中.用户可以触发执行程序,被触发后的程序就存进内存中成为一个个体,即为进程. 有些进程(比如crond需要每分钟都扫描.守护进程等等)是 ...

  9. 关于SpringMVC控制器的一点补充

    首先复习一下之前控制器的写法:http://www.cnblogs.com/eco-just/p/7882016.html. 我们可以看到,之前的写法是这样的: @RequestMapping(&qu ...

随机推荐

  1. Newton‘ method 的优缺点

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzE1Mjg5NQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  2. 求职(2015南京站获得百度、美的集团、趋势科技、华为offer)

    版权所有所有:没有马缰绳chhuach(CSDN博客源).转载请注明出处. 禁止www.haogongju.net转载. 特此声明 一.开篇: 9月底,找工作接近尾声,笔者主要经历了2015年南京站百 ...

  3. 【并查集+拓扑排序】【HDU1811】【Rank of Tetris】

    题意:给你3种关系 A=B,A>B,A<B 问是否排名方式唯一,或者存在矛盾 解 1.读入数据先处理 =号 用并查集的祖先作为代表元素,其他儿子节点都等于跟这个点重叠. 再读入 '< ...

  4. javascript模式——Mixin

    Mixin是一种扩展收集功能的方式,能提高代码的复用率. 在javascript中,原型可以继承于其它对象的原型,并且可以为任意数量的实例定义属性.可以利用这一点来促进函数的复用. 下面一段代码就是将 ...

  5. ./configure: error: the HTTP rewrite module requires the PCRE library

    docker中CentOS安装nginx出错,提示没有PCRE,需要安装pcre-devel,同时还需要安装openssl.openssl-devel yum -y install pcre-deve ...

  6. ios9配置info.plist中关于安全访问问题

    打开info.plist文件 - >添加App Transport Security Settings 字典类型,在App Transport Security Settings下增加Allow ...

  7. PHP中简单的图形处理

    PHP中简单的图形处理 计应134凌豪 1.加载GD库 GD库是一个开放的动态创建图像.源代码公开的函数库,可以从官方网站http://www.boutell.com/gd处下载.目前,GD库支持GI ...

  8. c语言 文件写入和读取

    #include<stdio.h> #include<stdlib.h> #include<string.h> #define N 10 struct studen ...

  9. poj中一些对我来说不错的东西(每天不同的加入,要保持)

    1.关于深度搜索与暴力结合的棋盘翻转问题 poj1753::2965:: 2.贪心算法:2109,2586: 3.韩信点兵问题:poj1006

  10. Eclipse配色插件

    1.打开Help -- Eclipse Marketplace 2.搜索Eclipse Color Theme,点击Install 3.安装完成后点击Window -- Preference -- A ...