首页 > Web开发 > 详细

ajax xml对象(比较少用,一般用json)

时间:2018-11-02 23:05:38      阅读:129      评论:0      收藏:0      [点我收藏+]

<%
@ page language="java" pageEncoding="utf-8" %> <!DOCYPE Html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>ajaxXml!!!</title> <script type="text/javascript"> function show(){ //获得ajax对象 var ajax; if(window.XMLHttpRequest){ ajax=new XMLHttpRequest(); }else if(window.AtiveXObject){ ajax=new AtiveXObject("Msxm12.XMLHTTP"); } //复写ajax.onreadystatechange ajax.onreadystatechange=function(){ if(ajax.readyState==4){ if(ajax.status==200){ //获得xml对象 var result=ajax.responseXML; alert(result.getElementsByTagName("name")[0].innerHTML); } } } //请求发送到servlet //ajax.open("get","ajaxXml"); //请求发送到xml.jsp ajax.open("get","xml.jsp"); ajax.send(null); } </script> <style type="text/css"> #div{ border:solid 2px; width:200px; height:100px; } </style> </head> <body> <div id="div"> <input type="button" value="show" onclick="show()" /> </div> </body> </html>
<%@ page language="java" contentType="text/xml; charset=utf-8" pageEncoding="utf-8" %>
<user>
    <uid>1</uid>
    <name>张三</name>
    <price>11.11</price>
</user>
public class AjaxXml extends HttpServlet{
    @Override
    public void service(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/XML;charset=utf-8");
        response.getWriter().write("<user><uid>1</uid><name>张三</name><price>11.11</price></user>");
        System.out.println("test");
    }
}
public class AjaxXml extends HttpServlet{
    @Override
    public void service(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/XML;charset=utf-8");
        response.getWriter().write("<user><uid>1</uid><name>张三</name><price>11.11</price></user>");
        System.out.println("test");
    }
}

 

ajax xml对象(比较少用,一般用json)

原文:https://www.cnblogs.com/lastingjava/p/9898647.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!