0

当我登录时,一切都像魅力一样,但是当匿名用户通过会话点击页面时,我收到以下错误:

SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符

我像这样使用 HttpSession 对象:

HttpSession session = request.getSession();

我使用对象getAttribuesetAttribute方法。

是否可以使用与匿名用户的会话,如果可以,我应该对我的代码进行哪些更改?

编辑:

显然问题出在ajax调用中。当我以匿名用户身份拨打电话时,我得到

意外的令牌 <

错误消息,而当我登录时一切正常。我发现当 ajax 调用中的 dataType 错误时会出现该消息。事实是,我的 dataType 是 json,我确实返回 json 作为响应。当以匿名用户身份进行调用时,根本不会调用响应该调用的控制器方法。当我将 dataType 更改为 html 时,我没有收到意外的令牌错误消息,但我的控制器方法仍然没有被调用,既不是匿名用户也不是经过身份验证的用户。

阿贾克斯调用:

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);
        }
    });
}

控制器方法:

@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响应:

<%@ 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>"
}
4

0 回答 0