使用ajax发起请求时候,响应的数据类型由ajax设置的接受数据方式决定:
responseText决定响应的数据类型是文本;
responseXML决定响应的数据类型xml文档。
以Servlet为例,当返回xml文档时候,Servlet一定要设置response.setContentType("text/xml;charset=utf-8")。根据是否需要缓存,可以设置response.setHeader("Cache-control","no-cache");
响应数据的格式由HTTP响应报文的Content-Type决定:
text/xml xml
text/html html
text/plain 文本
application/json json
下面是Servlet返回文本、xml文档、json、html的代码:
index.jsp
- <%@pagelanguage="java"contentType="text/html;charset=UTF-8"
- pageEncoding="UTF-8"%>
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
- <htmlheadMetahttp-equiv="Content-Type"content="text/html;charset=UTF-8"title>Inserttitlehere</scripttype="text/javascript"
- functiongetXMLHttpRequest(){
- varxmlReq;
- if(window.XMLHttpRequest){//codeforIE7+,Firefox,Chrome,Opera,Safari
- xmlReq=newXMLHttpRequest();
- }else{//codeforIE6,IE5
- newActiveXObject("Microsoft.XMLHTTP");
- }
- returnxmlReq;
- //返回文本数据测试
- functionajaxText(){
- varoReq=getXMLHttpRequest();
- oReq.open("POST","TextServlet",true);
- oReq.setRequestHeader("Content-type",
- "application/x-www-form-urlencoded");//提交表单必须
- oReq.send("name=我是文本&pwd=123");
- oReq.onreadystatechange=function(){
- if(oReq.readyState==4&&oReq.status==200){
- document.getElementById("myDiv").innerHTML=oReq.responseText;//responseText响应类型是文本
- }
- //返回XML文档测试
- functionajaxXML(){
- oReq.send("name=我是xml&pwd=111");
- varxmlObj=oReq.responseXML;//responseXML响应类型是XML文档
- varnames=xmlObj.documentElement.getElementsByTagName("name");
- varname=names[0].firstChild.nodeValue;
- varpwds=xmlObj.documentElement.getElementsByTagName("pwd");
- varpwd=pwds[0].firstChild.nodeValue;
- name+","+pwd;
- //返回json文档测试
- functionajaxJSON(){
- oReq.send("name=我是json&pwd=varresObj=oReq.responseText;//json字符串用文本形式接收
- varjsonObj1=eval('('+resObj+')');
- varname1=jsonObj1.name;
- varpwd1=jsonObj1.pwd;
- name1+","+pwd1;
- //返回html
- functionajaxHTML(){
- oReq.send("name=我是html&pwd=oReq.responseText;//html用文本形式接收
- resObj;
- scriptbodybuttononclick="ajaxText()">ajax请求文本button"ajaxXML()">ajax请求XML文档"ajaxJSON()">ajax请求json字符串"ajaxHTML()">ajax请求htmldivid="myDiv">div>
返回文本的Servlet:
[java]
copy
- packagecom.ajax.test;
- importjava.io.IOException;
- importjavax.servlet.ServletException;
- importjavax.servlet.ServletOutputStream;
- importjavax.servlet.http.HttpServlet;
- importjavax.servlet.http.HttpServletRequest;
- importjavax.servlet.http.HttpServletResponse;
- publicclassTextServletextendsHttpServlet{
- protectedvoiddoGet(HttpServletRequestrequest,HttpServletResponseresponse)throwsServletException,IOException{
- doPost(request,response);
- voiddoPost(HttpServletRequestrequest,IOException{
- System.out.println("intext");
- request.setCharacterEncoding("utf-8");
- Stringname=request.getParameter("name");
- Stringpwd=request.getParameter("pwd");
- "name:"+name+",pwd:"+pwd);
- //返回文本数据
- response.setContentType("text/plain;charset=utf-8");
- ServletOutputStreamoutputStream=response.getOutputStream();
- outputStream.write(name.getBytes("utf-8"));
- outputStream.write(pwd.getBytes("utf-8"));
- outputStream.flush();
- outputStream.close();
- }