在向服务器发送Ajax请求时如何避免HTTP状态415?

时间:2014-04-20 18:36:32

标签: java javascript ajax json

我有一个AJAX来电,应该返回JSON文件

function getData() {
    $.ajax({
        url: '/x',
        type: 'GET',
        data: "json",
        success: function (data) {
            // code omitted
        }
    });
}

我的服务器端很简单。

@RequestMapping(value = "/x", method = GET, produces = "application/json")
public @ResponseBody List<Employee> get() {
    return employeeService.getEmployees();
}

但我的要求甚至无法进入控制器。错误是:

HTTP Status 415 - The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.

我该如何解决?

1 个答案:

答案 0 :(得分:0)

在服务器端代码

之前添加@Consumes("text/html")
@Consumes("text/html")
@RequestMapping(value = "/x", method = GET, produces = "application/json")
public @ResponseBody List<Employee> get() {
    return employeeService.getEmployees();
}