1

我的代码:

<script type="text/javascript" src="JavaScript/jquery-1.9.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        $("#login").click(function() {
            var name1 = $("#eid").val();
            var pass1 = $("#p").val();
            var datacontent = {
                "empName": name1,
                "empPassword": pass1
            };

            alert(JSON.stringify(datacontent));

            $.ajax({
                type: "POST",
                accept: "application/json",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                url: "/rest_test1/empLogin.htm",
                data: JSON.stringify(datacontent), // Note it is important

                success: function(data) {
                    alert(data.empName + " " + data.empPassword);
                }
                // error: function(data, status, er) {
                //     alert("Data: " + data + "Status: " + status + "Error:" + er);
                // }
            });
        });
    });
</script>

控制器代码:

@Controller
public class EmployeeRestController {
    @RequestMapping(value = "/empLogin.htm", method = RequestMethod.POST)
    public @ResponseBody EmployeeVo doRegister(@RequestBody EmployeeVo employeeVo){
        System.out.println(employeeVo.getEmpName() + " " + employeeVo.getEmpPassword());
        return employeeVo;
    }
}

当我运行此代码时,值会显示在控制台中,但会出现错误:

406(不可接受)

所以下面的代码不起作用:

success: function(data) {
    alert(data.empName + " " + data.empPassword);
}

我使用jackson-annotations-2.3.0.jar, jackson-core-2.3.4.jar, jackson-databind-2.3.4.jarJar 文件。

4

1 回答 1

0

HTTP 状态码 406 表示服务器无法以请求accept标头规定的格式进行响应。换句话说,您的 Web 服务未配置为生成application/json响应。

尝试accept: "application/json"从您的 AJAX 请求中删除 ,或将内容类型添加到您的响应中,如下所示:

@RequestMapping(value="/empLogin.htm", headers="Content-Type=application/json", method=RequestMethod.POST)
于 2014-12-22T06:10:05.890 回答