简体   繁体   English

jQuery Ajax登录重定向

[英]Jquery ajax login redirect

I' have a simple popup login, which returns status into div(if pass is not correct) or redirect to the main page. 我有一个简单的弹出式登录名,它将状态返回到div(如果传递不正确)或重定向到主页。

Here is code for handling request: 这是处理请求的代码:

$.ajax({
     type: "POST",
     url: "login.html",
     cache: false,
     data: formData
     success: function(data){
        var $response=$(data);
        $('#response').text($response.find('#response').text());
        if($('#response').text().length<=0)
        {
            window.location="main.html";
        }
     }
 });

I'm using spring MVC for handling request. 我正在使用Spring MVC处理请求。 Login.html returns String(pass not correct). Login.html返回String(传递不正确)。 But when I get redirect from Spring nothing happens. 但是当我从Spring获得重定向时,什么也没有发生。 So I temporary added client code for redirect. 因此,我临时添加了用于重定向的客户端代码。

Is this approach correct? 这种方法正确吗? Cause I get success on both ways(wrong or correct password) 原因我在两种方法上都成功(密码错误或正确)

See when you redirect from Spring ,how your page will be redirected..because your doing an ajax submission..when you redirect and return home page from spring then you can see home page html in variable data of ajax call. 查看何时从Spring重定向,如何重定向页面。.由于执行ajax提交。.从Spring重定向并返回主页时,可以在ajax调用的可变数据中看到主页html。 Rule is simple..when you do ajax..browser normal submission wont interfere ..in ideal case when there is normal submission server will send HTTP code as 302 and your page will be redirected to new location(means new page opens). 规则很简单..当您执行ajax..browser时,正常提交不会造成干扰。.在理想情况下,当正常提交服务器将HTTP代码发送为302,并且您的页面将被重定向到新位置(意味着打开新页面)。

you can try this approach: 您可以尝试以下方法:

$.ajax({
     type: "POST",
     url: "login.html",
     cache: false,
     data: formData
     success: function(data){
       document.open();
       document.write(data);//home page from server after authentication..dont forget to add redirect logic at spring
       document.close();
     }
 });

I hope you are not using Spring-security for authentication..hence you may need to write lots of filter approach of your own. 我希望您不使用Spring-security进行身份验证。因此,您可能需要编写大量自己的过滤器方法。 Also instead of ajax try using normal submission approach from authetication..because in ajax you may need to set browser cookie with session id..of your own which would be taken care easily in normal authentication process..thanks 另外,也可以尝试使用来自身份验证的普通提交方法来代替ajax。因为在ajax中,您可能需要使用自己的会话ID来设置浏览器cookie。在正常身份验证过程中,这很容易注意。

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

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