Home > Web Front-end > JS Tutorial > How to use getJSON() to asynchronously request the server to return json format data implementation code

How to use getJSON() to asynchronously request the server to return json format data implementation code

黄舟
Release: 2017-06-04 10:48:37
Original
1515 people have browsed it

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);
    });
  }
Copy after login

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);
  }
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template