<%@ 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%>">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">

<title>This is my JSP 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">
-->
<style type="text/css">
div{
border: 1px solid red;
width: 400px;
height: 300px;
}
</style>
</head>

<body>
<input type="button" value="Ajax" onclick="testAjax()">
<div id="msg"></div>
</body>
<script type="text/javascript">

function testAjax(){
//document.getElementById("msg").innerHTML = "加载中...";
//id;
var request;
//创建 request对象
if(window.XMLHttpRequest){ //兼容性
request = new XMLHttpRequest();
}else if(window.ActiveXObject){ //针对IE
request = new ActiveXObject("Msxml2.XMLHTTP");
}

//写监听 去check request的状态
request.onreadystatechange = function(){
//
//console.log(request.readyState);
if(request.readyState == 4){
//得到 后台写出的数据

//当加载成功以后
if(request.status == 200){
var data = request.responseText;
document.getElementById("msg").innerHTML = data;
}else if(request.status == 404){
document.getElementById("msg").innerHTML = "资源没有找到";
}else if(request.status == 500){
document.getElementById("msg").innerHTML = "服务器错误";
}

}else{
document.getElementById("msg").innerHTML = "<img src=\"images/loading.gif\" />";
}
};

//打开请求
request.open("get", "ajax/ajaxController?name=李四&d="+new Date().getTime());

//发送数据
//如果没有数据 则写null 不然 其他浏览器可能会报错
request.send(null);
}

</script>
</html>

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
System.out.println(name);
try {
Thread.currentThread().sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//int c = 1/0;
//response.getWriter().write("Hello Ajax!!!");
response.getWriter().print(new Gson().toJson(new Users("lisi","123123")));
}

ajax基本常识及get请求方式的更多相关文章

  1. jQuery中ajax的4种常用请求方式

    jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...

  2. ajax中Post和Get请求方式的区别?

    ajax中Post和Get请求方式的区别: 1.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示. 2.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的 ...

  3. AJAX的get表单请求方式简述

    一般在页面中常用在表单的操作中,请求数据, action : 数据提交的地址,默认是当前页面 method : 数据提交的方式,默认是get方式 get: 把数据名称和数据值用=连接,如果有多个的话, ...

  4. Ajax中的get和post两种请求方式的异同

    Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别.   1. get是把参数数据队列加到提交表单的A ...

  5. jQuery中的Ajax几种请求方式

    1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...

  6. jquery ajax请求方式与提示用户正在处理请稍等,等待数据返回时loading的显示

    1.jquery ajax请求方式与提示用户正在处理请稍等 为了提高用户体验度,我们通常会给出 “正在处理,请稍等!”诸如此类的提示.我们可通过设置$.ajax()下的参数beforeSend()来实 ...

  7. AJAX中的请求方式以及同步异步的区别

    AJAX中的请求方式以及同步异步的区别请求方式,分为GET与POST: GET 最为常见的HTTP请求,普通上网浏览页面就是GET.GET方式的参数请求直接跟在URL后,以问号开始.(JS中用wind ...

  8. Ajax的get和post两种请求方式区别

    Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...

  9. AJAX请求方式

    <!DOCTYPE html PUBLIC "‐//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. bzoj1077

    题解 这道题n的范围很小,所以我们可以考虑枚举+判定 设放在天平右边的是C,D. 以A+B<C+D为例,因为差分约束必须是差的形式,所以我们将式子变形 B−C<D−A然后枚举D,A的取值, ...

  2. rabbitmq之window环境启动

    rabbitmq启动方式有2种 1.以应用方式启动 rabbitmq-server -detached 后台启动 Rabbitmq-server 直接启动,如果你关闭窗口或者需要在改窗口使用其他命令时 ...

  3. LNMP架构下Discuz论坛的搭建

    在上一节中,我们对lnmp架构下的mysql.php.nginx进行源码的安装,并设置了相关的安装参数.现在我们将在上一节的基础上,把三者联系起来进行一个论坛的部署. 一.首先进行Discuz(社区论 ...

  4. Java 异常Exception e中e的getMessage()和toString()方法的区别

    示例代码1: public class TestInfo { private static String str =null; public static void main(String[] arg ...

  5. Nginx下载和安装与启动

    nginx是什么 nginx是一个开源的,支持高性能,高并发的www服务和代理服务软件.它是一个俄罗斯人lgor sysoev开发的,作者将源代码开源出来供全球使用. nginx比它大哥apache性 ...

  6. CSS基础知识,学前准备

    1.引入层叠样式表: A.行内引入 <bodystyle="background-color:#cccccc">; 在标签内使用style属性 </body> ...

  7. Oracle联合查询

    select * from teacher--联合查询 --01.union (并集)select tno from teacher where tno>1080 union(select tn ...

  8. yarn workspaces基本试用

    初始化项目 yarn init -y 添加workspaces 支持 修改package.json { "name": "second", "vers ...

  9. FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的?

    FastAdmin 的 url 有一个 ref=addtabs 是怎么添加的? 在使用 FastAdmin 时你会发现 url 中有一个 ref=addtabs . 以下是 Karson 的解释 这个 ...

  10. 【转】Windows消息投递流程:WM_COMMAND消息流程

    原文网址:http://blog.csdn.net/hyhnoproblem/article/details/6182585 该示例通过研究基本的单文档程序的“文件”--“打开”命令,分析WM_COM ...