【问题标题】:Spring MVC + Ajax JSON postSpring MVC + Ajax JSON 帖子
【发布时间】:2017-01-03 22:21:21
【问题描述】:

我在将JSON 发送到Controller 时遇到问题。我无法理解我的问题。

所以,网址 - /notes/{username}/add

Ajax

$.ajax({
        type: "POST",
        contentType : 'application/json; charset=utf-8',
        dataType : 'json',
        url: window.location.pathname,
        data: JSON.stringify({ title: $("#title").val(), text: $("#text").val() }),
        success : function() {
            $("#title").val("");
            $("#text").val("");
        }
    });

控制器

@RequestMapping(value = "/{username}/add", method = POST)
    public void add(@RequestBody Note note) {
        noteRepository.add(new Note(UserSession.getUser(), note.getTitle(), note.getText()));
    }

注意

public class Note {

    private String title;
    private String text;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }
}

控制器没有收到来自 ajax 的请求。我认为,网址有问题,但我不知道为什么以及该怎么做。

【问题讨论】:

    标签: java json ajax spring spring-mvc


    【解决方案1】:

    将您的controller 修改为:

    @RequestMapping(value = "{username}/add", method = RequestMethod.POST)
    public void add(@RequestBody Note note, @PathVariable("username")String username) {
    }
    

    并且您的 ajax 调用 url 应该包含路径变量:

    $.ajax({
            type: "POST",
            contentType : 'application/json; charset=utf-8',
            dataType : 'json',
            url : '/Notes/notes/username/add',
            data: JSON.stringify({ title: $("#title").val(), text: $("#text").val() }),
            success : function() {
                $("#title").val("");
                $("#text").val("");
            }
        });
    

    还要确保您的 pom.xmlbuild.gradle 应该具有 jackson 依赖项,以下是 maven 项目的示例:

    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.13</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.3</version>
    </dependency>
    

    【讨论】:

    • 不幸的是,这对我没有帮助
    • 你的appapplicationcontext 名称是什么?
    • 将 ajax 调用中的 url 更改为 url : '/Notes/notes/username/add',
    • 再次没有帮助,我可以给你链接到 git 吗?
    • 您的应用能够构建,但在部署时会导致错误,无论如何我创建了一个示例来解决您的问题 - github.com/arpitaggarwal/spring-mvc-ajax,希望对您有所帮助!
    【解决方案2】:

    而不是像这样创建数据

    data: JSON.stringify({ title: $("#title").val(), text: $("#text").val() }),
    

    你可以尝试像这样序列化你的 html 表单

    <form name-"yourformname" id="formname">
    <input type="text" id="title" name=="title"/>
    <input type = "text" id="text" name="text"/>
    </form>
    <input type="button" value="Submit"/>
    

    在 Ajax 中这样做

        $.ajax({
                var formdata = $('#yourformname').serializeArray();
                type: "POST",
                contentType : 'application/json; charset=utf-8',
                dataType : 'json',
                url : '/Notes/notes/username/add',
                data: formdata,
                success : function() {
                    $("#title").val("");
                    $("#text").val("");
                }
            }
    

    );

    型号

    public class Note {
    
        private String title;
        private String text;
    
        public String getTitle() {
            return title;
        }
    
        public void setTitle(String title) {
            this.title = title;
        }
    
        public String getText() {
            return text;
        }
    
        public void setText(String text) {
            this.text = text;
        }
    }
    

    控制器

        @RequestMapping(value = "/{username}/add", method = POST)
    @ResponseBody
            public void add(Note note) { //In note instance you will get the json data
                //your code 
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 2018-02-01
      • 2013-01-18
      • 1970-01-01
      • 2018-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多