Home >Web Front-end >JS Tutorial >How to use getJSON() to asynchronously request the server to return json format data implementation code
The following editor will bring you an implementation of using getJSON() to asynchronously request the server to return json format data. The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.
We can use the getJSON() method of jquery to request the server to return json format data:
js code:
function test(){ $.getJSON("JsonServlet",function(result){ alert(result.name); }); }
Server-side servlet response:
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String person="{\"name\":\"zhangsan\",\"sex\":\"man\",\"age\":\"23\"}"; resp.getWriter().print(person); }
Note: json dataAttributesNames and character values must be in double quotes
The above is the detailed content of How to use getJSON() to asynchronously request the server to return json format data implementation code. For more information, please follow other related articles on the PHP Chinese website!