简体   繁体   中英

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.

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. Login.html returns String(pass not correct). But when I get redirect from Spring nothing happens. 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. 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).

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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