Can I use HttpSession objects with anonymous user in AppFuse Spring MVC project?

352 views Asked by At

When I'm logged in everything works like a charm, but when anonymous user hits the page with session I get the following error:

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

I use HttpSession object like this:

HttpSession session = request.getSession();

and I use object's getAttribue and setAttribute methods.

Is it possible to use sessions with anonymous users, and if so, what should I change to my code?

Edit:

Obviously the problem is in ajax call. When I make that call as anonymous user I get

unexpected token <

error message, while when I'm logged in everything goes fine. I found that message appears when dataType in ajax call is wrong. The fact is, that my dataType is json, and I do return json as response. When call is made as anonymous user, the controller method that responds to that call is not called at all. When I change dataType to html I don't get that unexpected token error message, but my controller method is still not called, neither as anonymous or authenticated user.

Ajax call:

function incrementNormalAjax(issueId) {
    jQuery.ajax({
        url: "/ycexams-web/showIssue/incrementNormal",
        type: "POST",
        data: { issueId: issueId } ,
        contentType:"application/x-www-form-urlencoded",
        dataType:"json",
        success: function(ajaxResponse){

            if(ajaxResponse.newPercentage_html != "")
            {
                $("#issuePercentage").html(ajaxResponse.newPercentage_html);
            }
            else
            {
                $("#issuePercentage").html("zajebava");
            }
        },
        error: function(xhr, status, error) {
            alert(error);
        }
    });
}

Controller's method:

@RequestMapping(value = "/showIssue/incrementNormal", method = RequestMethod.POST)
public String incrementNormal(final HttpServletRequest request, @RequestParam(value = "issueId") Long issueId, ModelMap model) {
    HttpSession session = request.getSession();
    List<Long> votedIssues = (List<Long>) session.getAttribute("votedIssues");
    votedIssues.add(issueId);

    Issue issue = issueManager.get(issueId);
    issue.setNormal(issue.getNormal() + 1);
    issueManager.save(issue);
    int normal = issue.getNormal();
    int notNormal = issue.getNotNormal();
    int newPercentage = normal * 100 / (normal + notNormal);
    model.addAttribute("newPercentage", newPercentage);

    return "newPercentageJson";
}

json response:

<%@ page trimDirectiveWhitespaces="true" contentType="application/json"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
{
     "newPercentage_html":  "<spring:escapeBody javaScriptEscape="true">
         <c:if test="${not empty newPercentage}">
            ${newPercentage}%
         </c:if>
    </spring:escapeBody>"
}
0

There are 0 answers