关于一个Ajax传一个对象到后台的问题,帮帮带学人

form.on('submit(add)',
            function() {                                              //看看能不能构成单个学生成绩的工具类StuGradetool3
                var Scores = [];
                var Cnames = [];
                var Score2 = [];
                Score2 = [[${Coursename}]];
                for(var i =0;i<[[${Coursename.size()}]];i++){
                    Scores.push((parseFloat($('#C_id'+Score2[i]).val()))),   //拿到输入的成绩
                    Cnames.push(Score2[i]);
                }
                var stuGradetool3 = {
                    "S_id" : $('#S_id').val(),
                    "name" : $('#S_name').val(),
                    "cnames" : Cnames,
                    "scores" : Scores,
                }
                console.log(stuGradetool3);              //这个有截图
                $.ajax({
                    url : '/Grade-add-function',                      //发送异步请求
                    type : 'post',
                    //使用JSON格式
                    dataType : 'json',
                    data : stuGradetool3,
                    success :  function (data){
                        console.log(data);
                    }
                })
                layer.alert("增加成功", {
                        icon: 6
                    },
                    function() {
                        //关闭当前frame
                        xadmin.close();

                        // 可以对父窗口进行刷新
                        xadmin.father_reload();
                    });
                return false;
            });

下面放输入和console打印的截图

Controller代码

@RequestMapping("/Grade-add-function")
    @ResponseBody
    public boolean toAdd_Grade(StuGradetool3 grade){
        System.out.println("------------------------------------------------");

        System.out.println("fuck");
        int len = grade.getCnames().size();
        System.out.println(len);
       System.out.println(grade.getS_id()+"|"+grade.getName());
        for(int i =0;i<len;i++){
            System.out.println(grade.getCnames().get(i)+"|"+grade.getScores().get(i));
        }
        for(int i = 0;i<len;i++){
            gradeService.insert_Grade(grade.getS_id()
                    ,courseService.search_grade_id_byname(grade.getCnames().get(i))
                    ,grade.getScores().get(i));
        }
        return true;
    }

报错截图

如果要页面代码在下面

<form class="layui-form" th:action="@{/Grade-add-function}">
            <div class="layui-form-item">
                <label for="S_id" class="layui-form-label">
                    <span class="x-red">*</span>学号</label>
                <div class="layui-input-inline">
                    <input type="text" id="S_id" name="ssid" required="" lay-verify="student_id" autocomplete="off" class="layui-input"></div>
                <div class="layui-form-mid layui-word-aux">
                    <span class="x-red">*</span>学生学号</div></div>
            <div class="layui-form-item">
                <label for="S_name" class="layui-form-label">
                    <span class="x-red">*</span>学生姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="S_name" name="ssname" required="" lay-verify="nikename" autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item"  th:each="course:${Coursename}">
                <label for="S_id" class="layui-form-label" th:text="${course+':'}">
                    <span class="x-red">*</span></label>
                <div class="layui-input-inline">
                    <input type="text" th:id="${'C_id'+course}"  required="" lay-verify="" autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label  class="layui-form-label"></label>
                <button class="layui-btn" lay-filter="add" lay-submit="">增加</button></div>
        </form>

错误信息

ERROR 20040 --- [nio-8080-exec-7] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.NumberFormatException: For input string: ""] with root cause

java.lang.NumberFormatException: For input string: ""

 

查看全部
jamenu
寻找罗红
2020/11/27 22:43
  • spring
  • java
  • intellij-idea
  • ajax
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复