Home  >  Article  >  Web Front-end  >  Operation steps for using getJSON() to asynchronously request the server to return json data

Operation steps for using getJSON() to asynchronously request the server to return json data

php中世界最好的语言
php中世界最好的语言Original
2018-03-31 15:32:391199browse

This time I will bring you the steps of using getJSON() to asynchronously request the server to return json data. What are the precautions for using getJSON() to asynchronously request the server to return json data. The following is a practical case. , let’s take a look. 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 believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How to use ajax to submit comments and automatically refresh them


How to use ajax to pass an array to the background

The above is the detailed content of Operation steps for using getJSON() to asynchronously request the server to return json data. 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