我的代码:
<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.jar
Jar文件。
答案 0 :(得分:0)
HTTP状态代码406表示服务器无法以请求的accept
标头规定的格式进行响应。换句话说,您的网络服务未配置为生成application/json
响应。
尝试从您的AJAX请求中删除accept: "application/json"
,或将内容类型添加到您的回复中,如下所示:
@RequestMapping(value="/empLogin.htm", headers="Content-Type=application/json", method=RequestMethod.POST)