Home  >  Article  >  Web Front-end  >  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

黄舟
黄舟Original
2017-06-04 10:48:371490browse

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!

Statement:
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