使用getJSON()异步请求服务器返回json格式数据

我们可以使用jquery的getJSON()方法请求服务器返回json格式数据:

js代码:

function test(){
        $.getJSON("JsonServlet",function(result){
            alert(result.name);
        });
    }


服务器端servlet响应:

@Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String person="{"name":"zhangsan","sex":"man","age":"23"}";
        resp.getWriter().print(person);
    }

注意:json数据属性名和字符型值都必须是双引号

原文地址:https://www.cnblogs.com/javayuan/p/5388985.html