zoukankan      html  css  js  c++  java
  • jsp Ajax请求(返回json数据类型)

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
      
        <title>My JSP  page</title>
        
        <script>
            
            
            function addText(){
                //创建XMLHttpRequest 请求的对象
                var request = new XMLHttpRequest();
                // 请求的方式
                var method = "GET";
                // 请求的连接地址
                var url = "AServlet";
                // 初始化请求参数
                request.open(method, url,true);
                // 发送请求
                request.send(null);
                request.onreadystatechange = function(){
                    // 如果响应请求
                    if(request.readyState==4){
                        // 如果请求成功
                        if(request.status==200 || request.status==304){
                            
                            var text = request.responseText;
                            alert(text);
                            var data = eval("("+text+")");
                            /* var data = JSON.parse(text); */
                            alert(data);
                            var span = document.getElementsByTagName("span")[0];
                            span.innerHTML = span.innerHTML+data.user.address.province;
                        }
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
      
    
      <servlet>
        <servlet-name>AServlet</servlet-name>
        <servlet-class>ajax.AServlet</servlet-class>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>AServlet</servlet-name>
        <url-pattern>/AServlet</url-pattern>
      </servlet-mapping>
      
    </web-app>
    
    
    package ajax;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class AServlet extends HttpServlet {
    
        private static final long serialVersionUID = 1L;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            
            System.out.println("doGet...");
            //设置编码
            request.setCharacterEncoding("utf-8");
            response.setCharacterEncoding("utf-8");
            //设置返回文本的类型
            response.setContentType("text/json");
            
            String jsonString = "{"user":{"name":"lf","address":{"province":"广东","city":"深圳"}}}";
            
            response.getWriter().print(jsonString);
                    
            
        }
        
        
        
        
    
    }
            }
                };
                
            }
    
        </script>
        
      </head>
      
      <body>
          <h1>解析xml</h1>
          <span>省份:</span><br>
        <a href="#" onclick="addText()">添加文本</a>
      </body>
    </html>
  • 相关阅读:
    ARM Linux 3.x的设备树(Device Tree)
    ubuntu 14.04 编译内核出现unable to locate package ncurses-devel 问题的解决
    Device Tree Usage( DTS文件语法)
    Ubuntu 14.04中gedit打开文件出现中文乱码问题
    Jenkins中集成jmeter-maven插件
    Linux(centos6.5)下安装jenkins
    IM系统架构设计之浅见
    一些常用软件的网络端口协议分类介绍
    Jenkins执行批处理文件失败
    八大持续集成工具
  • 原文地址:https://www.cnblogs.com/lantu1989/p/6285854.html
Copyright © 2011-2022 走看看