Json对象在ajax成功时是空的

时间:2013-07-05 20:26:07

标签: ajax json jquery

我正在使用objectMapper从控制器返回一个Json对象。但是,成功的是显示数据是空的。无法找到解决方案。请帮助

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;

        }

1 个答案:

答案 0 :(得分: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返回的discussionService.updateVotesByQuestionId(id, votedByUserId, action)是否为空