2013-07-05 63 views
0

我使用objectMapper從控制器返回一個Json對象。但成功顯示數據是空的。無法找到解決方案。請幫助json對象在ajax成功時爲空

function voteAction(questionId, votedByUserId, action) { 
    $.ajax({ 
     url: '/voteQuestion?questionId=' + questionId + '&votedByUserId=' + votedByUserId + '&action=' + action, 
     contentType: 'application/json', 
     dataType: 'json', 
     async: false, 
     success: function response(data) { 
      console.info("data in vote action is :: " + data); 
      var votesCount = data.votesCount; 
      alert("votes count::" + votesCount); 
      var status = data.status; 
      console.log(status + " :: is the status"); 
      if (status == 10) { 
       $("#voice-box").fadeIn(); 
       document.getElementById("statusMessage").innerHTML = "You've already VotedUp for this Question"; 
       document.getElementById("voice-box").setAttribute("status", "saved"); 
       setTimeout("hideStatusMessage()", 1950); 
      } 
      else if (status == -10) { 
       $("#voice-box").fadeIn(); 
       document.getElementById("statusMessage").innerHTML = "You've already VotedDown for this Question"; 
       document.getElementById("voice-box").setAttribute("status", "saved"); 
       setTimeout("hideStatusMessage()", 1950); 
      } 
      else { 
       if (status == 10) { 
        $("#voice-box").fadeIn(); 
        document.getElementById("statusMessage").innerHTML = "You've successfully Voted for this Question"; 
        document.getElementById("voice-box").setAttribute("status", "saved"); 
        setTimeout("hideStatusMessage()", 1950); 
       } 
      } 
      $('#questionVotes h3').html(votesCount); 
     } 
    }); 
} 

這是我的控制器

@RequestMapping(value="/voteQuestion",method=RequestMethod.GET) 
public @ResponseBody void voteQuestion(@RequestParam("questionId")String id,@RequestParam("votedByUserId")String votedByUserId,@RequestParam("action")String action,HttpServletResponse response) throws JsonProcessingException, IOException, Throwable 
{ 
     response.getWriter().println(objMapper.writeValueAsString(discussionService.updateVotesByQuestionId(id, votedByUserId, action))); 
    } 

這是我的服務方法。

public static Map<String, Integer> updateVotesByQuestionId(String questionId,String votedByUserId, String action) 
    { 
     int voteCount=0; 
     int status=0; 
     System.out.println("action=="+action); 
     Map<String,Integer> returnMap=new HashMap <String,Integer>(); 
     String responseString=""; 
     Questions question=questionsDAO.getQuestionByQuestionId(Long.parseLong(questionId)); 
     List<String> votedUpUsersList = question.getVotedUpUserIds(); 
     List<String> votedDownUsersList = question.getVotedDownUserIds(); 
     if(action.equals("voteUp")){ 
     if(votedUpUsersList.contains(votedByUserId)) 
     {  
       status=10; 
     } 
     else{ 

      if(votedDownUsersList.contains(votedByUserId)) 
      { 
      votedDownUsersList.remove(votedByUserId); 
      question.setVotedDownUserIds(votedDownUsersList); 

      } 
     votedUpUsersList.add(votedByUserId); 
     question.setVotedUpUserIds(votedUpUsersList); 

     responseString=questionsDAO.saveQuestion(question); 
     }} 
     else if(action.equals("voteDown")){ 
      if(votedDownUsersList.contains(votedByUserId)){ 
        status=-10; 

      } 
      else 
      { 

        if(votedUpUsersList.contains(votedByUserId)) 
        { 
        votedUpUsersList.remove(votedByUserId); 
        question.setVotedUpUserIds(votedUpUsersList); 

        } 
       votedDownUsersList.add(votedByUserId); 
       question.setVotedDownUserIds(votedDownUsersList); 

       responseString=questionsDAO.saveQuestion(question); 
       } 
     } 
     if(responseString == "Success"){ 
      voteCount= question.getVotedUpUserIds().size()-question.getVotedDownUserIds().size(); 
      returnMap.put("voteCount",voteCount); 
      returnMap.put("status",status); 

     } 

     return returnMap; 

     } 
+0

您是否使用過類似Chrome開發人員工具網絡標籤的內容來查看請求實際上正在響應的內容? –

+0

那麼......也許你沒有從服務器返回任何東西。我們怎麼知道? [學習如何**調試** JavaScript](http://www.netmagazine.com/tutorials/javascript-debugging-beginners)。 –

+0

您需要先確認服務器正在響應某種內容。然後,您必須確保響應內容類型實際上是JSON。 –

回答

0

從方法返回的JSON對象的字符串

public @ResponseBody String voteQuestion(...) 

代替

public @ResponseBody void voteQuestion(...) 

全法

@RequestMapping(value="/voteQuestion",method=RequestMethod.GET) 
public @ResponseBody String voteQuestion(@RequestParam("questionId")String id, 
       @RequestParam("votedByUserId")String votedByUserId,@RequestParam("action")String action) throws JsonProcessingException, IOException, Throwable 
{ 
    return objMapper.writeValueAsString(discussionService.updateVotesByQuestionId(id, votedByUserId, action)); 
} 

還檢查該Map由返回不爲空