nullpointer

30byixjq  于 2021-06-23  发布在  Mysql
关注(0)|答案(2)|浏览(318)

我在从前端引导向expressservlet发送json数据时遇到了问题。这是我试过的。我使用ajax将数据从表单发送到带有json的servlet类。请帮我解决这个问题。这是我的学生登记表:

<html>
<head>
<link rel="stylesheet" type="text/css" href="resources/css/bootstrap.min.css">
<script type="text/javascript" src="resources/js/jquery.min.js"></script>
<script type="text/javascript" src="resources/js/bootstrap.min.js">   </script>
<script type="text/javascript">
    function registerStudent() {
        var firstName = $("#firstName").val();
        var lastname = $("#lastname").val();
        var phone = $("#phone").val();
        var email = $("#email").val();
        var studentCode = $("#studentCode").val();
        $.ajax({
            type : "POST",
            url : "/RegisterStudent",
            data :{data: "firstName=" + firstName + "lastname=" + lastname + " phone=" +  phone + "email=" + email+"studentCode"+studentCode},
            success : function(data) {
                var ht = data.msg;
                $("#resp").html(ht);
            },
            error : function(data) {
                alert("Some error occured.");
            }
        });
    }
</script>
</head>
<body>

<div class="container">

<div class="card " style="width:750px;margin:0px auto">

    <div class="card-header">Student Register</div>

    <div class="card-body">

        <form data-toggle="validator" role="form" method="post" >

            <div class="form-group">

                <label class="control-label" for="firstName">Name</label>

                <input class="form-control" data-error="Please enter name field." id="firstName" placeholder="Name"  type="text" required />

                <div class="help-block with-errors"></div>

            </div>

            <div class="form-group">

                <label for="lastname" class="control-label">lastname</label>

                <input type="text" class="form-control"   id="lastname" placeholder="lastname" required>

                <div class="help-block with-errors"></div>

            </div>

            <div class="form-group">

                <label for="phone" class="control-label">phone</label>

                <div class="form-group">

                    <input type="text" data-minlength="5" class="form-control" id="phone" data-error="fill your phone" placeholder="phone" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>

            <div class="form-group">

                <label class="control-label" for="email">email</label>

                <textarea class="form-control" data-error="Please enter email field." id="email" placeholder="email" required=""></textarea>

                <div class="help-block with-errors"></div>

            </div>

            <div class="form-group">

                <label for="studentCode" class="control-label">studentCode</label>

                <div class="form-group">

                    <input type="text"  class="form-control" id="studentCode" data-error="fill your studentCode" placeholder="studentCode" required>

                    <div class="help-block with-errors"></div>

                </div>

            </div>

        </form>
    </div>
    <button onclick="registerStudent();" type="button" class="btn btn-success btn-block">Register</button>
    <div class="text-center" id="resp" style="margin-top: 14px;"></div>
</div>
</div>

这是我的servlet类,用于将数据发送到数据库。

@WebServlet(urlPatterns = "/RegisterStudent")
public class RegisterStudentController extends HttpServlet {
StudentServiceInter service = new StudentServiceImpl();
ObjectMapper mapper = new ObjectMapper();
String json = "";

protected void doGet(HttpServletRequest req, HttpServletResponse resp)    throws ServletException, IOException {
    Student student = new Student();
    req.getSession().setAttribute("student", student);
    resp.sendRedirect("/addStudent.jsp");
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String jsonData = req.getParameter("data");
    resp.setContentType("application/json");
    PrintWriter out = resp.getWriter();
    out.println(jsonData);
    out.close();
}
}
jaql4c8m

jaql4c8m1#

试试这个。。。

$.ajax({
                            type: 'GET',
                            url: '<?php echo base_url(); ?>"/RegisterStudent"', // your syntax
                            data: {
                                'firstName ': firstName ,
                                'lastName'  :lastname,
                                'phone': phone,
                                'email' : email,
                                'studentCode' : studentCode,

                            },
                           success : function(data) {
                    var ht = data.msg;
                    $("#resp").html(ht);
                },
                error : function(data) {
                    alert("Some error occured.");
                }
                        });
3wabscal

3wabscal2#

一个问题是,客户端没有创建json,而服务器端尝试解析json。json格式没有 equal (=) 签字。
有效对象是:

data = {
    data: {
        firstName: 'Name',
        lastName: 'Surname',
        phone: '123-4456',
        email: 'a@b.c',
        studentCode: 12345
    }
};

在发送之前 JSON.stringify(data) .

相关问题