ajax基本常识及get请求方式
<%@ 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请求方式的更多相关文章
- jQuery中ajax的4种常用请求方式
jQuery中ajax的4种常用请求方式: 1.$.ajax()返回其创建的 XMLHttpRequest 对象. $.ajax() 只有一个参数:参数 key/value 对象,包含各配置及回调函数 ...
- ajax中Post和Get请求方式的区别?
ajax中Post和Get请求方式的区别: 1.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示. 2.Post传输的数据量大,可以达到2M,而Get方法由于受到URL长度的 ...
- AJAX的get表单请求方式简述
一般在页面中常用在表单的操作中,请求数据, action : 数据提交的地址,默认是当前页面 method : 数据提交的方式,默认是get方式 get: 把数据名称和数据值用=连接,如果有多个的话, ...
- Ajax中的get和post两种请求方式的异同
Ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别. 1. get是把参数数据队列加到提交表单的A ...
- jQuery中的Ajax几种请求方式
1. load( url, [data], [callback] ) :载入远程 HTML 文件代码并插入至 DOM 中. url (String) : 请求的HTML页的URL地址. data (M ...
- jquery ajax请求方式与提示用户正在处理请稍等,等待数据返回时loading的显示
1.jquery ajax请求方式与提示用户正在处理请稍等 为了提高用户体验度,我们通常会给出 “正在处理,请稍等!”诸如此类的提示.我们可通过设置$.ajax()下的参数beforeSend()来实 ...
- AJAX中的请求方式以及同步异步的区别
AJAX中的请求方式以及同步异步的区别请求方式,分为GET与POST: GET 最为常见的HTTP请求,普通上网浏览页面就是GET.GET方式的参数请求直接跟在URL后,以问号开始.(JS中用wind ...
- Ajax的get和post两种请求方式区别
Ajax的get和post两种请求方式区别 (摘录):http://ip-10000.blog.sohu.com/114437748.html 解get和post的区别. 1. get是把参数数据队列 ...
- AJAX请求方式
<!DOCTYPE html PUBLIC "‐//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
随机推荐
- HDU 4687 Boke and Tsukkomi 一般图匹配,带花树,思路,输出注意空行 难度:4
http://acm.hdu.edu.cn/showproblem.php?pid=4687 此题求哪些边在任何一般图极大匹配中都无用,对于任意一条边i,设i的两个端点分别为si,ti, 则任意一个极 ...
- iOS笔记之ScrollView
ScrollView三个基本的要理解的属性: contentSize The size of the content view.所以很好理解,contentSize也就是scrollView可滚动的区 ...
- Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本
Howto: 在ArcGIS10中将地图文档(mxd文档)批量保存到之前版本 文章编号 : 38783 软件: ArcGIS - ArcEditor 10 ArcGIS - ArcInfo 10 A ...
- L175 Endorestiform Nucleus: Scientist Just Discovered a New Part of the Human Brain
The newly named Endorestiform Nucleus sits in the inferior cerebellar小脑 peduncle, at the junction be ...
- New Concept English Two 27 73
新概念一:对应小学水平,重在口语和基础.是不可多得的学习教材,全本144课,可以给孩子(hello world 级别的我)学半年.新概念二:对应高中水平,重在听力和场景对话,共96课,学后,听懂歪果仁 ...
- 仿智能社官网:原生JS实现简单又酷炫的3D立方体时钟
先放一下我做的效果:https://linrunzheng.github.io/3Dclock/3Dclock/new.html 至于3D立方体怎么做这里就不在阐述了,可以看一下我之前的博客. 这里默 ...
- Yii Model
REFs 自动生成代码 创建第一个Yii应用 创建模型 Yii 用户登陆机制
- zend 2.2 db select 使用例子
<?php use Zend\Db\Sql\Select; // basic table $select0 = new Select; $select0->from('foo'); // ...
- CI框架------codeIgniter
之前学习了thinkphp,学完之后印象不太深刻,在网上询问了一下,他们都说多学几个框架,以后可以自己写框架. 于是自己就放下thinkphp,下定决心再学一个,于是又从网上看了几个框架,综合比较了一 ...
- 10055 - Hashmat the Brave Warrior & 各数据类型所占字节数 (C语言)
Problem A Hashmat the brave warrior Input: standard input Output: standard output Hashmat is a brave ...