【发布时间】:2019-01-28 22:35:12
【问题描述】:
我是 Spring Boot 框架的新手。 我有一个 jQuery 方法,它在单击 span 元素时被调用。在那个 jQuery 方法中,我有一个 ajax 调用,它调用 Spring Boot 控制器并将字符串作为参数传递。控制器正在获取从 ajax 调用传递的参数。但它并没有重定向到不同的视图。不同的视图是一个名为 ajaxView.html 的 html 页面。我也想为视图添加一个属性。任何帮助都会很棒。
这是我的 ajax 调用:
$(document).ready(function() {
$('.spanClass').on('click', '#id_1', function(event){
event.stopPropagation();
var str = ((event.target.parentElement).parentElement.href);
$.ajax({
type: 'POST',
url: "http://localhost:8080//span/view?term="+encodeURIComponent(str),
contentType: 'text/plain',
crossDomain: false,
async:true,
success:function(response) {
}
});
});
});
这是我的控制器方法:
@RequestMapping(method=RequestMethod.POST, value = "/span/view")
public @ResponseBody ModelAndView redirectAjax(String term, Model model) {
Employee emp = new Employee();
emp.setName(term);
model.addAttribute("employee", emp);
return new ModelAndView("redirect:/ajaxView");
}
【问题讨论】:
-
您不能从 AJAX 重定向到不同的 PAGE。您只需要通过脚本来处理它。 stackoverflow.com/questions/19346430/…
标签: java jquery ajax spring-boot thymeleaf