Spring MVC + Jquery Ajax显示406错误?

我已经搜索了很多,无法找到解决方案。类似的线程也在我们的堆栈溢出。但没有用。所以我创建了一个新的线程。

我的JSP是,

  

Spring_JsonTest Example

Enter your name :
Your Education :

我的Jquery是,

 function doAjaxPost() { // get the form values var name = $('#name').val(); var education = $('#education').val(); $.ajax({ type: "POST", url: contexPath + "/AddUser.html", data: "name=" + name + "&education=" + education, success: function(response){ // we have the response if(response.status == "SUCCESS"){ userInfo = "
    "; for( i =0 ; i < response.result.length ; i++){ userInfo += "
  1. Name : " + response.result[i].name + "; Education : " + response.result[i].education; } userInfo += "
"; $('#info').html("User has been added to the list successfully. " + userInfo); $('#name').val(''); $('#education').val(''); $('#error').hide('slow'); $('#info').show('slow'); }else{ errorInfo = ""; alert("Success response : "+response) for( i =0 ; i < response.result.length ; i++){ errorInfo += "
" + (i + 1) +". " + response.result[i].code; } $('#error').html("Please correct following errors: " + errorInfo); $('#info').hide('slow'); $('#error').show('slow'); } }, error: function(xhr, ajaxOptions, thrownError) alert("status :"+xhr.status+"\nthrownError : "+thrownError+"\nresponseText : "+xhr.responseText); alert('Error: ' +xhr.description); } }); }

我的控制器是,

 @Controller public class UserController { private List userList = new ArrayList(); @RequestMapping(value="/AddUser.html",method=RequestMethod.POST) public @ResponseBody JsonResponse addUser(@ModelAttribute(value="user") User user, BindingResult result ){ System.out.println("Add user POST method"); JsonResponse res = new JsonResponse(); ValidationUtils.rejectIfEmpty(result, "name", "Name can not be empty."); ValidationUtils.rejectIfEmpty(result, "education", "Education not be empty"); if(!result.hasErrors()){ userList.add(user); res.setStatus("SUCCESS"); res.setResult(userList); }else{ res.setStatus("FAIL"); res.setResult(result.getAllErrors()); } return res; } } 

我的JsonResponse.java是,

 package com.tps.jsonTest.service; public class JsonResponse { private String status = null; private Object result = null; public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public Object getResult() { return result; } public void setResult(Object result) { System.out.println("Result : "+result.toString()); this.result = result; } } 

和我的User.java是,

 package com.tps.jsonTest.service; public class User { private String name = null; private String education = null; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getEducation() { return education; } public void setEducation(String education) { this.education = education; } } 

在我的控制台中,

 Add user POST method Name : Anand Education : BE Result : [com.tps.jsonTest.service.User@df2940] 

我用过的jar文件是,

 commons-logging-1.1.1.jar jackson-core-asl-1.9.12.jar jackson-mapper-asl-1.9.12.jar log4j-1.2.9.jar servlet-api-2.4.jar spring-aop-3.2.2.RELEASE.jar spring-beans-3.2.2.RELEASE.jar spring-context-3.2.2.RELEASE.jar spring-context-support-3.2.2.RELEASE.jar spring-core-3.2.2.RELEASE.jar spring-expression-3.2.2.RELEASE.jar spring-web-3.2.2.RELEASE.jar spring-webmvc-3.2.2.RELEASE.jar 

在我的浏览器中,错误是,

在此处输入图像描述

我无法使用jquery解析对象,它会移动错误。希望我们的堆栈用户可以帮助我。

很好的答案肯定是赞赏。谢谢。

这是Spring 3.2.2或3.2.3或3.2.4中的问题。 我在Spring 3.0.0和Spring 3.1.4中尝试过相同的例子它运行正常。 所以改变弹簧jar并试一试。

关心Madhu

我遇到过同样的问题。 我使用Spring 3.2和UrlBasedViewResolver。

对于所有ajax调用,它给出了406.我尝试更改标题,dataType以上解决方案但没有任何效果。 然后我发现jacksonjar子不见了。 我在下面添加了两个jar子,它开始适合我。

jackson-core-asl-1.9.4.jar jackson-mapper-asl-1.9.4.jar

您的问题是请求不接受响应的内容类型。

您的ajax调用未指定dataType ,因此jQuery根据url扩展推断响应应该是内容类型text/html ,但是您的服务器不知道如何将pojo转换为html!

解决方案应该是简单地将dataType='json'添加到您的ajax调用中。

希望这可以帮助。

由于您正在进行POST ,因此您的数据不应采用查询字符串格式。 相反,为data参数传递json对象。

 data:{name: name, education: education}