【问题标题】:Spring MVC and AJAX errorSpring MVC 和 AJAX 错误
【发布时间】:2013-11-10 19:25:31
【问题描述】:

在 JSP 文件中,我有输入学生组的字段,然后将此数据传递给实体类 GroupStudent(没有 getter 和 setter 方法)

@Entity
@Table(name = "GroupStudent")
@NamedQueries({ 
@NamedQuery(name = "GroupStudent.getAllGroups", // get all groups
            query = "select g from GroupStudent g"),
@NamedQuery(name = "GroupStudent.getGroupByName", // get group by name
            query = "select g from GroupStudent g where g.groupStudentNumber = :name")
})
public class GroupStudent implements Serializable {
public GroupStudent() {}

public GroupStudent(String groupStudentNumber) {
    this.groupStudentNumber = groupStudentNumber;
}
// table GroupStudent fields
private Long groupStudentId;
private String groupStudentNumber;
}

JSP

<label>Group</label>
<input id="groupStudentNumber"/>
<input type="submit" value="Add" onclick="addGroupAjax()" />

和ajax函数将数据传递给Spring Controller

function addGroupAjax() {
            var groupStudentNumber = $('#groupStudentNumber').val();

            $.ajax({
                type: "POST",
                url: "/IRSystem/addData.html",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                data: "{groupStudentNumber:" + groupStudentNumber + "}",
                success: function(response) {

                },
                error: function(e) {
                    alert("Error" + e);
                }
            });
        } 

但它不会将数据传递给控制器​​。实体的字段为空。

@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody Student addNewGroup(@ModelAttribute(value = "group") GroupStudent group) {

    System.out.println("Entered group: " + group.getGroupStudentNumber());

    return new Student();
}

还有一件事我也不能将实体 Student 传递给 ajax。我添加到 Spring jars 中

jackson-core-asl-1.7.1 和 jackson-mapper-asl-1.7.1 能够将实体对象传递给 ajax。但它没有给出结果。当我尝试在 Google Chrome 中传递数据(到 ajax)时,我的窗口出现错误 Error[object Object]。 我不知道为什么会这样。如有任何信息,我将不胜感激,谢谢。

【问题讨论】:

  • 向我们展示GroupStudent类的样子。
  • 将 ajax 函数调用中的 url 更改为 url:"addData.html" 我的意思是删除 /IRSystem/
  • 我添加了 GroupStudent 类。
  • jdev,如果我删除那部分我会得到错误 404。

标签: java ajax json spring-mvc


【解决方案1】:

您的控制器未正确设置为接受该帖子。如果您要发布 JSON 正文,则需要为方法参数使用 @RequestBody 注释,如下所示:

@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody Student addNewGroup(@RequestBody GroupStudent group) {
    System.out.println("Entered group: " + group.getGroupStudentNumber());
    return new Student();
}

这样,您的 JSON 将直接映射到您的 GroupStudent 对象。

【讨论】:

  • 你是对的,这是我的错误,我读过它,但忘了改变。但是当我尝试将数据从 ajax 传递到控制器或从控制器传递到 ajax 时(如果我在没有任何 @RequestBody 参数的情况下将数据传递给 ajax),我仍然有来自浏览器的错误 Error[object Object]。
【解决方案2】:

您还需要通过在 @RequestMapping 中添加 consumes=MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE 来指定使用媒体类型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-09
    • 1970-01-01
    • 2013-05-29
    • 2014-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-25
    相关资源
    最近更新 更多