0

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>"
}
legionar
  • 75
  • 2
  • 8
  • 1
    You're probably getting a 403 error and it's being returned as HTML rather than JSON. I'd recommend using Chrome's Developer Tools to inspect the response and see what it looks like. Also, I'd recommend you use @ResponseBody in your Controller to return the JSON directly, rather than building it in a JSP page. – Matt Raible Dec 20 '14 at 19:42
  • @Matt I'm getting a 302 Moved Temporarily status. Do you have any idea why is that? – legionar Dec 20 '14 at 21:17
  • Hey, I found it, I have /showIssue* allowed for anonymous, but not and /showIssue/incrementNormal. That was the problem. Thanks a lot @Matt! :) – legionar Dec 20 '14 at 21:29

0 Answers0