zoukankan      html  css  js  c++  java
  • 在servlet中返回json数据

    在servlet:

    String name = new tring(request.getParameter("name").getBytes("iso8859-1"),"utf-8");
    response.setCharacterEncoding("utf-8");
    request.setCharacterEncoding("utf-8");
    response.setContentType("application/json; charset=utf-8");
     //拼接json数据
    String jsonStr = "{"name":""+name+"","age":"20"}";
     //将数据写入流中
    response.getWriter().write(jsonStr);

    js:

    <script>
        $(function(){
            $.post("${ pageContext.request.contextPath }/servlet/jsonServlet",{"name":"赵子龙"},function(result){
                            //遍历
                $.each(result, function(i, field){
                  $("#divbox").append(i+":"+field + ";");
                });
            });
            
    //         $.getJSON("${ pageContext.request.contextPath }/servlet/jsonServlet",function(result){
    //             $.each(result, function(i, field){
    //               $("#divbox").append(i+":"+field + ";");
    //             });
            
    //         }); 
            
        })
        </script>            
  • 相关阅读:
    五一集训——图论
    Luogu P3942 将军令
    8.14 Round 1
    8.10 Round 1
    8.9 Round 1
    8.7 Round 2
    8.7 Round 1
    8.6 Round 1
    8.5 Round 2
    FHQ-Treap
  • 原文地址:https://www.cnblogs.com/tongxuping/p/7049564.html
Copyright © 2011-2022 走看看