AJAX第一步:AJAX接收返回类型为text/html的字符串数据

在一个javaweb项目中,可能会用到网页的局部刷新,下面我将自己总结的使用ajax进行网页刷新的知识点记录下来。请求方式有get和post方式两种,下面我分别进行了示范。

jsp页面代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!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=UTF-8">
<title>简易ajax 返回类型为Text/html</title>
</head>
<body>
<input type="button" οnclick="ajaxGETMethod()" value="ajax1点击"><div id="ajax1">111</div>
<input type="button" οnclick="ajaxPOSTMethod()" value="ajax2点击"><div id="ajax2">222</div>
</body>
 
<!--  ajax进行局部·刷新 -->
<script type="text/javascript">
function ajaxGETMethod(){
	//声明对象
	var xmlhttp;
//创建对象
if(window.XMLHttpRequest){
	xmlhttp = new XMLHttpRequest();
}else{
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
//请求成功的话  此方法会接收来自servlet的返回数据
xmlhttp.onreadystatechange=function() {
	//如果成功请求 并且成功接受响应
	if(xmlhttp.readyState==4 && xmlhttp.status==200){
		//id为ajax1的里面内容 设置为servlet返回的内容
		document.getElementById("ajax1").innerHTML = xmlhttp.responseText;
	}
}
//设置请求method方式为get  路径  以及是否异步  get方式的Content-Type: text/plain;charset=UTF-8
xmlhttp.open("get","${pageContext.request.contextPath}/AjaxDemo_TextHTML?cmd=ajax1您好",true);
 //发送请求
xmlhttp.send(); 
}
</script>

 
<script type="text/javascript">

function ajaxPOSTMethod(){
	var xmlhttp;
if(window.XMLHttpRequest){
	xmlhttp = new XMLHttpRequest();
}else{
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
	if(xmlhttp.readyState==4 && xmlhttp.status==200){
		document.getElementById("ajax2").innerHTML = xmlhttp.responseText;
	}
}
 //设置请求method方式为post  路径  以及是否异步  post方式的Content-Type: application/x-www-form-urlencoded
xmlhttp.open("post","${pageContext.request.contextPath}/AjaxDemo_TextHTML",true);
//设置请求头
 xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 //发送请求
xmlhttp.send("cmd=ajax2您好");
 
}
</script>
</html>
servlet页面代码:

package cn.sdut.ajaxdemo;

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;

@WebServlet("/AjaxDemo_TextHTML")
public class AjaxDemo_TextHTML extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//设置返回类型text/html 和编码格式 使中文不乱码
		response.setContentType("text/html;charset=utf-8");
		//获得请求消息
		String cmd = request.getParameter("cmd");System.out.println(cmd);
		//响应
		response.getWriter().write(cmd);
	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		         //设置返回类型text/html 和编码格式 使中文不乱码
		        request.setCharacterEncoding("utf-8");
				response.setContentType("text/html;charset=utf-8");
				//获得请求消息
				String cmd = request.getParameter("cmd");System.out.println(cmd);
				//响应
				response.getWriter().write(cmd);
	
	}

}
注意:浏览器会有缓存,所以每次ajax请求相同数据时,浏览器可能会给你显示的是缓存,要想不缓存 可以在请求后面加上一个时间参数,比如:
xmlhttp.open("post","${pageContext.request.contextPath}/AjaxDemo_TextHTML?date="+Date().getTime(),true);




  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值