Home  >  Article  >  Web Front-end  >  jQuery+json makes Ajax calling function (with code)

jQuery+json makes Ajax calling function (with code)

php中世界最好的语言
php中世界最好的语言Original
2018-04-25 15:45:111364browse

This time I will bring you jQuery json to make Ajax calling function (with code). What are the precautions for jQuery json to make Ajax calling function. The following is a practical case, let’s take a look.

Userservlet.java code:

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 userList = new ArrayList();
  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();
 }
}

Html code:


 帐号 jquery 密码 ajax
 
  
       
  •     帐号         
  •    
  •     密码         
  •    
  •         
  •   
 
 查询到的数据  

 

 

UserServlet Remember to import the tool class JSONStringObject JSONUtil

jsp To import jquery.js and json .js

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:

Detailed explanation of python reading and writing json files (with code)

Case using JSONP induction

The above is the detailed content of jQuery+json makes Ajax calling function (with 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