当我调用ajax时,它会进入控制器,然后作为响应,它将执行错误功能。
ajax代码:-
<script>
function showEmpDetails(empid)
{
console.log(empid);
$.ajax({
url: "employeeInfo",
type: "POST",
data: { empId: empid} ,
success: function(data) {
console.log(data);
$("#employeeDetails").html(data);
},
error: function () {
alert("error");
}
});
}
</script>
ajax控件将转到控制器,并且还将打印Hello。 控制器:-
@RequestMapping(value = "/employeeInfo", method = RequestMethod.POST,produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody String empInfo(@RequestParam("empId") int employeeId, Model model)
{
Employee findById = employeeMapper.findById(employeeId);
model.addAttribute("employee", findById);
System.out.println("Hello");
return "EMPINFO";
}
答案 0 :(得分:0)
我认为您需要在ajax调用中将 dataType 设置为“文本”。
<script>
function showEmpDetails(empid) {
console.log(empid);
$.ajax({
url: "employeeInfo",
type: "POST",
dataType: 'text',
data: { empId: empid },
success: function (data) {
console.log(data);
$("#employeeDetails").html(data);
},
error: function () {
alert("error");
}
});
}
</script>