在ajax中,总是会调用错误函数。如何解决此问题?

时间:2019-08-30 09:08:04

标签: jquery ajax spring-mvc

当我调用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";
    }

1 个答案:

答案 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>