【发布时间】: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