Ajax——响应内容为xml

前端之家收集整理的这篇文章主要介绍了Ajax——响应内容为xml前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
响应内容为xml数据
* 服务器端:
> 设置响应头:ContentType,其值为:text/xml;charset=utf-8
* 客户端:

> var doc = xmlHttp.responseXML;//得到的是Document对象!

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
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 'ajax5.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">
function createXMLHttpRequest(){
	try {
		return new XMLHttpRequest();
	} catch (e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveObject("MicroSoft.XMLHTTP");
			} catch (e) {
				// TODO: handle exception
			}
		}
	}
}
window.onload=function(){
	var bt=document.getElementById("bt");
	bt.onclick=function(){
		//1.创建异步对象
		var xmlHttp=createXMLHttpRequest();
		//2.打开与服务器的连接
		xmlHttp.open("GET","<c:url value='/BServlet' />",true);
		//3.发送请求
		xmlHttp.send(null);
		//4.给onreadystatechange上注册监听器
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4 && xmlHttp.status==200){
				var doc=xmlHttp.responseXML;
				// 查询文档下名为student的所有元素,得到数组,再取下标0元素
				var ele = doc.getElementsByTagName("student")[0];
				var number = ele.getAttribute("number");//获取元素名为number的属性值
				var name=ele.getElementsByTagName("name")[0].text;
				var age=ele.getElementsByTagName("age")[0].text;
				var sex=ele.getElementsByTagName("sex")[0].text;
				var text=number + "," + name + "," + age + "," + sex;
				var h1=document.getElementById("h1");
				h1.innerHTML=text;
		}
		};
	}
};
</script>
  </head>
  
  <body>
    <button id="bt">点击这里</button> 
    <h1 id="h1"></h1>
  </body>
</html>

package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class BServlet extends HttpServlet {


	public void doGet(HttpServletRequest request,HttpServletResponse response)
			throws ServletException,IOException {

		response.setContentType("text/xml;charset=utf-8");
		
		String xml=("<students>"+
					"<student number='001'>"+
					"<name>ZhangSan</name>"+
					"<age>18</age>"+
					"<sex>mail</sex>"+
					"</student>"+
					"</students>");
		response.getWriter().print(xml);
	}

}
原文链接:https://www.f2er.com/ajax/162382.html

猜你在找的Ajax相关文章