【发布时间】:2015-08-30 16:16:07
【问题描述】:
好的,我正在通过 jQuery 向我的 Spring Controller 提交一个简单的表单。代码如下:
<form id="addNote" role="form" action="addNote" method="POST">
<div class="form-group error">
<label for="note">Enter your Note </label>
<textarea rows="3" cols="50" name="note" class="form-control"
id="note"></textarea>
</div>
<div class="alert alert-danger" style="display: none">
<strong>Error!</strong> Text length must not exceed 100 characters
</div>
<button type="submit" class="btn btn-default">Save Note</button>
$(document).ready(function(){
$("#addNote").submit(function(e){
e.preventDefault();
var formObj = $(this);
var formURL = "addNote";
var formData = $("#note").val();
$.ajax({
url: formURL,
type: 'POST',
data: formData,
success: function(response){
alert(response);
},
error: function(response){
}
});
});
});
以及Controller方法:
@RequestMapping(value="/addNote", method=RequestMethod.POST)
public Vector<String> addNote(Locale locale, Model model, HttpServletRequest httpServletRequest, HttpSession httpSession){
String note = httpServletRequest.getParameter("note");
notes.add(note);
ModelAndView mv = new ModelAndView("notes");
mv.addObject("thenotes", notes);
return notes;
}
我需要将 notes 对象返回给 jQuery,以便我可以将其数据显示为输出。但这是我在 Chrome 控制台中遇到的错误:
所以显然路径中存在问题。我尝试在 jQuery 中将var formURL = "addNote"; 更改为var formURL = "assessment/addNote";,但它不起作用。
但由于某种原因,如果我将控制器中addNote() 函数的返回值更改为ModelAndView 并返回mv,那么它可以工作,但这不是我需要的 jQuery 中的响应。
【问题讨论】:
标签: java jquery ajax spring spring-mvc