【问题标题】:In ajax always error function is call. How to fix this problem?在 ajax 中总是调用错误函数。如何解决这个问题?
【发布时间】:2019-08-30 09:08:04
【问题描述】:

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

【问题讨论】:

  • 如果error 处理程序被击中,那么问题出在您的服务器端逻辑上。检查开发工具中的请求以查看确切的响应。希望它会给您一个调试错误。我可以看到的一个问题是您正在发送 empId 但该属性被命名为 employeeId
  • 还要查看error 处理程序接收到的参数,因为它们为您提供了更多信息。

标签: jquery ajax spring-mvc


【解决方案1】:

我认为您需要在 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-12
    • 1970-01-01
    相关资源
    最近更新 更多