2013-04-10 262 views
0

我已經搜索了很多,找不到解決方案。類似的線程也在我們的堆棧overflow.But沒有use.So我創建了一個新的線程。Spring MVC + Jquery Ajax顯示406錯誤?

我的JSP是,

<boby> 
<% System.out.println("request.getContextPath() : "+request.getContextPath()); %> 
<h1>Spring_JsonTest Example</h1> 
    <table> 
     <tr><td colspan="2"><div id="error" class="error"></div></td></tr> 
     <tr><td>Enter your name : </td><td> <input type="text" id="name"><br/></td></tr> 
     <tr><td>Your Education : </td><td> <input type="text" id="education"><br/></td></tr> 
     <tr><td colspan="2"><input type="button" value="Add Users" onclick="doAjaxPost()"><br/></td></tr> 
     <tr><td colspan="2"><div id="info" class="success"></div></td></tr> 
    </table>  
</body> 

我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 = "<ol>"; 
      for(i =0 ; i < response.result.length ; i++){ 
       userInfo += "<br><li><b>Name</b> : " + response.result[i].name + 
       ";<b> Education</b> : " + response.result[i].education; 
      } 
      userInfo += "</ol>"; 
      $('#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 += "<br>" + (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<User> userList = new ArrayList<User>(); 

    @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 : B.E 
Result : [[email protected]] 

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 

在瀏覽器中的錯誤是,

enter image description here

我不能使用jquery解析對象,它會在error.Hope我們的堆棧用戶將幫助我。

好的答案肯定讚賞。謝謝。

+0

你能後的圖像中的錯誤?這很難讀... – Thihara 2013-04-10 10:40:20

+0

@Thihara'狀態:406','拋出錯誤:undefined'。 – 2013-04-10 10:43:05

+1

你可以嘗試設置內容類型爲文本/ json – Satya 2013-04-10 10:43:52

回答

0

既然你做了POST,那麼你的數據不應該是查詢字符串格式。改爲傳遞data參數的json對象。

data:{name: name, 
     education: education} 
+0

沒有限制,您不能傳遞POST請求的url參數。 – Akshay 2013-04-20 19:35:28

1

您的問題是請求的內容類型不可接受。

您的ajax調用沒有指定dataType,所以jQuery推斷基於url擴展的響應應該是內容類型text/html,但您的服務器不知道如何將pojo轉換爲html!

解決方案應該是簡單地將dataType='json'添加到您的ajax調用中。

希望這會有所幫助。

3

這是Spring 3.2.2或3.2.3或3.2.4中的一個問題。我已經在Spring 3.0.0和Spring 3.1.4中嘗試過相同的例子,它工作正常。所以改變春天的罐子,試試看。

問候 馬杜

+0

是的 - 我正在下載文件沒有麻煩,直到我試圖從3.1.4升級到4.0.0。降級到3.2.0也失敗了。我將試着弄清楚是否還有其他需要改變的東西。 – Steve 2014-01-08 12:32:11

2

我有同樣的問題。我正在使用Spring 3.2和UrlBasedViewResolver。

對於所有的ajax調用它給了406.我試着改變頭,dataType所有上述解決方案,但沒有任何工作。然後我發現傑克遜罐子丟失了。 我在2個罐子下面添加了,它開始適合我。

傑克遜核心ASL-1.9.4.jar 傑克遜映射器-ASL-1.9.4.jar