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>            
  • 相关阅读:
    面向对象
    Vue + webpack 项目实践
    配置webstorm监听less时时转换
    delegate事件代理
    JS for 循环
    JS while 循环
    JS switch 分支语句
    JS if 判断
    JS 运算符
    JS typeof() parseInt() parseFloat()
  • 原文地址:https://www.cnblogs.com/tongxuping/p/7049564.html
Copyright © 2011-2022 走看看