我的代码:
<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 文件。