Home Web Front-end JS Tutorial Simple Ajax call example implemented by jQuery+json_jquery

Simple Ajax call example implemented by jQuery+json_jquery

May 16, 2016 pm 03:25 PM
ajax call jquery json

The example in this article describes the simple Ajax call implemented by jQuery+json. Share it with everyone for your reference, the details are as follows:

Userservlet.java code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

package com.iss.servlet;

import org.json.JSONException;

import org.json.JSONObject;

import com.iss.pojo.User;

import com.iss.util.JSONUtil;

public class UserServlet extends HttpServlet {

 public void doGet(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

  doPost(request, response);

 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)

   throws ServletException, IOException {

  response.setContentType("text/html;charset=utf-8");

  //list 添加对象

  List<User> userList = new ArrayList<User>();

  User user1 = new User("张三", "男", "18");

  User user2 = new User("李四", "男", "19");

  User user3 = new User("王五", "男", "20");

  userList.add(user1);

  userList.add(user2);

  userList.add(user3);

  PrintWriter out = response.getWriter();

  String str = null;

  try {

   //帐号密码如果匹配则把list 返回给界面

   if (request.getParameter("userName").equals("jquery")

     && request.getParameter("password").equals("ajax")) {

    str = JSONObject.quote(JSONUtil.toJSONString(userList));

   }

   out.print(str);

  } catch (JSONException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  System.out.println("str "+str);

  out.flush();

  out.close();

 }

}

Copy after login

Html code:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

<body>

 帐号 jquery 密码 ajax

 <form id="mainform">

  <ul>

   <li>

    帐号

    <input type="text" name="userName" />

   </li>

   <li>

    密码

    <input type="password" name="password" />

   </li>

   <li>

    <input onclick="login()" type="button" value="登录" />

   </li>

  </ul>

 </form>

 查询到的数据

 <div id="diva">

 </div>

 <script type="text/javascript">

   function login(){

   //获取form的参数

   var args =$("#mainform").serialize();

   //调用 jquery 的json获取方法

   //三个参数分别为 :请求路径 ,请求参数,返回数据的回调函数

   $.getJSON("servlet/UserServlet",args,function (data){

   if(data!=null){

   // 界面返回的是一个json格式字符串 调用JSON.parse()把数据转化为json

   // 格式的对象

   var jsondata =JSON.parse(data);

   parseData(jsondata);

   }else{

   alert("帐号密码输入有误");

   }

   })

   }

   function parseData(data){

   var str="";

   //遍历json格式数据

   for (var key in data){

   strstr =str+" 用户"+data[key].userName+" 年龄"+data[key].age+"<br/>"

   alert(str);

   }

   //把数据添加到div中

   $("#diva").html(str);

   }

  </script>

</body>

Copy after login

UserServlet remember to import the tool class JSONStringObject JSONUtil

jsp needs to import jquery.js and json.js

I hope this article will be helpful to everyone in jQuery programming.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Performance optimization tips for converting PHP arrays to JSON Performance optimization tips for converting PHP arrays to JSON May 04, 2024 pm 06:15 PM

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

How do annotations in the Jackson library control JSON serialization and deserialization? How do annotations in the Jackson library control JSON serialization and deserialization? May 06, 2024 pm 10:09 PM

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

jQuery Tips: Quickly modify the text of all a tags on the page jQuery Tips: Quickly modify the text of all a tags on the page Feb 28, 2024 pm 09:06 PM

Title: jQuery Tips: Quickly modify the text of all a tags on the page In web development, we often need to modify and operate elements on the page. When using jQuery, sometimes you need to modify the text content of all a tags in the page at once, which can save time and energy. The following will introduce how to use jQuery to quickly modify the text of all a tags on the page, and give specific code examples. First, we need to introduce the jQuery library file and ensure that the following code is introduced into the page: &lt

In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese In-depth understanding of PHP: Implementation method of converting JSON Unicode to Chinese Mar 05, 2024 pm 02:48 PM

In-depth understanding of PHP: Implementation method of converting JSONUnicode to Chinese During development, we often encounter situations where we need to process JSON data, and Unicode encoding in JSON will cause us some problems in some scenarios, especially when Unicode needs to be converted When encoding is converted to Chinese characters. In PHP, there are some methods that can help us achieve this conversion process. A common method will be introduced below and specific code examples will be provided. First, let us first understand the Un in JSON

Quick tips for converting PHP arrays to JSON Quick tips for converting PHP arrays to JSON May 03, 2024 pm 06:33 PM

PHP arrays can be converted to JSON strings through the json_encode() function (for example: $json=json_encode($array);), and conversely, the json_decode() function can be used to convert from JSON to arrays ($array=json_decode($json);) . Other tips include avoiding deep conversions, specifying custom options, and using third-party libraries.

How to tell if a jQuery element has a specific attribute? How to tell if a jQuery element has a specific attribute? Feb 29, 2024 am 09:03 AM

How to tell if a jQuery element has a specific attribute? When using jQuery to operate DOM elements, you often encounter situations where you need to determine whether an element has a specific attribute. In this case, we can easily implement this function with the help of the methods provided by jQuery. The following will introduce two commonly used methods to determine whether a jQuery element has specific attributes, and attach specific code examples. Method 1: Use the attr() method and typeof operator // to determine whether the element has a specific attribute

Summary of commonly used file operation functions in PHP Summary of commonly used file operation functions in PHP Apr 03, 2024 pm 02:52 PM

目录1:basename()2:copy()3:dirname()4:disk_free_space()5:disk_total_space()6:file_exists()7:file_get_contents()8:file_put_contents()9:filesize()10:filetype()11:glob()12:is_dir()13:is_writable()14:mkdir()15:move_uploaded_file()16:parse_ini_file()17:

How to use PHP functions to process JSON data? How to use PHP functions to process JSON data? May 04, 2024 pm 03:21 PM

PHP provides the following functions to process JSON data: Parse JSON data: Use json_decode() to convert a JSON string into a PHP array. Create JSON data: Use json_encode() to convert a PHP array or object into a JSON string. Get specific values ​​of JSON data: Use PHP array functions to access specific values, such as key-value pairs or array elements.

See all articles