Ajax实现自动填充信息(jQuery实现Ajax案例)报500错误的解决方法以及eval函数解决不显示的问题

代码如下

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<script type="text/javascript" src="js/jquery-1.8.0.min.js"></script>
<script type="text/javascript">
   $(function (){
     
       $("#uid").blur(function (){
     
           //1、获取value值
           var uid = $(this).val();
           //2、发送请求
           $.post("/getuser","uid="+uid,function (rs){
     
               //转换成可以处理的json
               rs = eval("("+rs+")")
               //前端接收到的rs就是我们后台发送过来的json格式对象
               $("#uname").val(rs.username);
               $("#pass").val(rs.password);
               $("#money").val(rs.money)
           })
       })
   })
</script>
<body>
   uid:<input type="text"id="uid"><br>
   name:<input type="text"id="uname"><br>
   pass:<input type="text"id="pass"><br>
   money:<input type="text"id="money"><br>
</body>
</html>
import net.sf.json.JSONObject;

import javax.json.JsonObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
@WebServlet(urlPatterns = "/getuser")
public class GetUsersServlet extends HttpServlet {
     
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
       doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
     
        //接收参数
        int uid = Integer.parseInt(req.getParameter("uid"));
        User user = null;
        switch (uid){
     
            case 1:
                user = new User(1,"张三","abc","33");
                break;
            case 2:
                user = new User(2,"张三2","abc","33");
                break;
            case 3:
                user = new User(3,"张三3","abc","33");
                break;
            case 4:
                user = new User(4,"张三4","abc","33");
                break;
            default:user = new User();
        }
        //将Java对象转换为json对象
        JSONObject jsonObject = JSONObject.fromObject(user);//json对象
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println(jsonObject);
    }
}

public class User {
     
    private Integer uid;
    private String username;
    private String password;
    private String money;

    public User() {
     
    }

    public User(Integer uid, String username, String password, String money) {
     
        this.uid = uid;
        this.username = username;
        this.password = password;
        this.money = money;
    }

    @Override
    public String toString() {
     
        return "User{" +
                "uid=" + uid +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", money='" + money + '\'' +
                '}';
    }

    public Integer getUid() {
     
        return uid;
    }

    public void setUid(Integer uid) {
     
        this.uid = uid;
    }

    public String getUsername() {
     
        return username;
    }

    public void setUsername(String username) {
     
        this.username = username;
    }

    public String getPassword() {
     
        return password;
    }

    public void setPassword(String password) {
     
        this.password = password;
    }

    public String getMoney() {
     
        return money;
    }

    public void setMoney(String money) {
     
        this.money = money;
    }
}

我们要用到的工具
Ajax实现自动填充信息(jQuery实现Ajax案例)报500错误的解决方法以及eval函数解决不显示的问题_第1张图片
下面我们说一下我们在运行时可能遇到的错误

第一个错误就是500:这个错误是什么意思呢,说我们找不到相应的类:解决方案是这样得:我们将我们所需要的工具包复制到我们的Tomcat的lib文件夹下:
这就要求我们的小伙伴要记住我们tomcat的安装路径
Ajax实现自动填充信息(jQuery实现Ajax案例)报500错误的解决方法以及eval函数解决不显示的问题_第2张图片

Ajax实现自动填充信息(jQuery实现Ajax案例)报500错误的解决方法以及eval函数解决不显示的问题_第3张图片
运行的时候我们还可能遇到就是我们明明输入了序号也失去了焦点,但是我们input下面就是没有显示我们的数据:解决方案如下
我们在拿到后台发给我们的结果rs之后(json格式的对象)
需要将rs转换为我们能处理的json格式并重新赋给rs

rs= eval("("+rs+")")

这样子我们就可以解决这个问题了

你可能感兴趣的:(javaweb,Ajax,jquery,500)