Javascript 在Spring MVC中重新更正
我有一个在js中获取的方法Javascript 在Spring MVC中重新更正,javascript,java,spring,spring-mvc,jsp,Javascript,Java,Spring,Spring Mvc,Jsp,我有一个在js中获取的方法 $(".btn-sm").click(function() { $.ajax({ url: '/check_rating/'+this.value, type: 'GET', contentType: 'application/x-www-form-urlencoded; charset=UTF-8', success: function (response
$(".btn-sm").click(function() {
$.ajax({
url: '/check_rating/'+this.value,
type: 'GET',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
alert(response.status);
},
error: function () {
alert("error");
}
});
});
url是这样的/check\u rating/1。控制器
@RequestMapping(value = "/check_rating/{id}",method = RequestMethod.GET)
public String check_rating(@PathVariable("id")Long id, RedirectAttributes redirectAttributes){
List<Rating>rating = ratingService.findAllRatingsByIdStudentAndStageOfApproveGreaterThan(id,0);
redirectAttributes.addFlashAttribute("rating",rating);
return "redirect:/students_rating";
}
@RequestMapping(value = "/students_rating",method = RequestMethod.GET)
public String student_rating(@ModelAttribute("rating") List<Rating>rating, ModelMap model){
model.addAttribute("rating",rating);
return "students_rating";
}
}
students\u rating.jsp
<tbody id="tBody">
<c:forEach items="${requestScope.rating}" var="rating">
<tr><td class="column"><c:out value="${rating.id}"></c:out></td><td><c:out value="${rating.date}"></c:out></td><td><c:out value="${rating.score}"></c:out></td></tr>
</c:forEach>
</tbody>
您不能为ajax请求重定向带有位置标头的页面。您必须为ajax获取一些响应文本,并使用解析的响应文本重定向用户。这是如何用JS代码重定向浏览器
if (responseText == 'OK') {
document.location.href = 'http://example.com/';
}
如果您使用AJAX请求点击控制器,那么您的SpringMVC控制器应该返回一些响应,并且基于响应,您可以在视图页面中重定向请求 将ajax调用更新为以下代码:
$(".btn-sm").click(function() {
$.ajax({
url: '/check_rating/'+this.value,
type: 'GET',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
if (responseText == 'OK') {
window.location.href = '<--replace this with controller URL-->';
}
},
error: function () {
alert("error");
}
});
});
$(“.btn sm”)。单击(函数(){
$.ajax({
url:'/check_rating/'+this.value,
键入:“GET”,
contentType:'application/x-www-form-urlencoded;charset=UTF-8',
成功:功能(响应){
如果(responseText=='OK'){
window.location.href='';
}
},
错误:函数(){
警报(“错误”);
}
});
});
您可以使用document.location.href,但在html5中不推荐使用它。此文件的可能副本无法解决我的问题请参见
$(".btn-sm").click(function() {
$.ajax({
url: '/check_rating/'+this.value,
type: 'GET',
contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
success: function (response) {
if (responseText == 'OK') {
window.location.href = '<--replace this with controller URL-->';
}
},
error: function () {
alert("error");
}
});
});