简体   繁体   English

如何让已配置的会话在AJAX中工作

[英]How to let my configured session work in AJAX

I have this interceptor function where I configure my session. 我在配置会话时具有此拦截器功能。

if (request.getRequestURL().indexOf("profile") > 0) {
    if (session.getAttribute("access").equals("sub-admin")) {
        System.out.println("This is request to profile - admin");
    } else {
        System.out.println("This is request to profile - user");
        response.sendRedirect(request.getContextPath() + "/error"); //ERROR HERE YOU ARE JUST A USER NOT AN ADMIN, I WILL REDIRECT YOU TO ERROR PAGE
    }
}

Now I am using jQuery and AJAX in my front end. 现在,我在前端使用jQuery和AJAX。

If I am just a user and I will access localhost:8080/sample/profile , It will work. 如果我只是一个用户,并且我将访问localhost:8080/sample/profile ,它将起作用。 It redirected me to the error page. 它将我重定向到错误页面。

But, when I access it in my menu in the home page and click profile, it doesn't work. 但是,当我在主页的菜单中访问它并单击配置文件时,它不起作用。

I think it is because I am using AJAX and the path doesn't change, the view only. 我认为这是因为我正在使用AJAX,并且路径不会改变,仅视图是不变的。

$.ajax({
    url: ROOT_URL + '/sample/profile',
    type: "get",
    dataType: "text"
}).done(function(data) {
    $('#idcontainer').html(data);
});

How do you let the session work in my AJAX front end? 您如何让会话在我的AJAX前端工作?

If you'd like to handle the redirect from an AJAX call, you can take a look at the following question: 如果您想处理来自AJAX调用的重定向,则可以查看以下问题:

A better solution might be to check if the request is AJAX, and send a JSON response with an HTTP status that you can handle on the frontend: 更好的解决方案可能是检查请求是否为AJAX,然后发送可在前端处理的HTTP状态的JSON响应:

JSON Response: JSON响应:

{
    "error": "Unauthorized",
    "message": "You must be logged in to view this content",
    "code": 401
}

And in your interceptor: 在拦截器中:

boolean ajax = "XMLHttpRequest".equals(request.getHeader("X-Requested-With"));
if (ajax) {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
    response.getWriter().write(responseToClient);
    response.getWriter().flush();
    response.getWriter().close();
} else {
    response.sendRedirect(request.getContextPath() + "/error");
}

Note that not all AJAX libraries include the X-Requested-With header, but jQuery along with most other modern libraries do. 请注意,并非所有AJAX库都包含X-Requested-With标头,但是jQuery和大多数其他现代库都包含。

Then in your JavaScript function: 然后在您的JavaScript函数中:

$.ajax({
    url: ROOT_URL + '/sample/profile',
    type: "get",
    dataType: "text"
}).done(function(data) {
    // handle success HTML
}).fail(function (data) {
    // parse JSON and alert error message
});

In addition, it seems that you're using the AJAX request to replace the contents of the page with the HTML returned from the AJAX request. 另外,似乎您正在使用AJAX请求将页面内容替换为AJAX请求返回的HTML。 Instead of using a JSON response, you could also just return the error HTML instead and display that in the same way that you are returning the profile HTML content: 除了使用JSON响应之外,您还可以返回错误HTML,并以与返回配置文件HTML内容相同的方式显示该错误:

HTML response: HTML响应:

<h1>Error</h1>

<p class="error">You must be logged in to view this content.</p>

And set the HTML the same way as in your done callback: 并以与done回调相同的方式设置HTML:

.fail(function (data) {
    $('#idcontainer').html(data);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM