Home  >  Article  >  Web Front-end  >  getJSON() asynchronously requests the server to return json format data (graphic tutorial)

getJSON() asynchronously requests the server to return json format data (graphic tutorial)

亚连
亚连Original
2018-05-22 14:00:011778browse

Below I will bring you an implementation of using getJSON() to asynchronously request the server to return json format data. Let me share it with you now and give it as a reference for everyone.

We can use jquery's getJSON() method 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: Both json data attribute names and character values ​​are required It’s double quotation marks

. I compiled the above for everyone. I hope it will be helpful to everyone in the future.

Related articles:

Explanation of Ajax asynchronous request technology examples

HTTP messages and ajax basic knowledge

Ajax characteristics and garbled code problems (graphic tutorial)

The above is the detailed content of getJSON() asynchronously requests the server to return json format data (graphic tutorial). 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