简体   繁体   中英

nodejs express session is undefined when posting from ajax

I've read and tried all the previous questions on the topic before posting here. I have many pages all of which work, in the same manner(sessions are saved) But i have this one page where it isn't and i can't figure out why...

I have this function:

function saveuser(){     
     uId = $('#userId').val();
     f = $('#firstName').val();
     l = $('#lastName').val();
     u = $('#firstname').val();
     p = $('#password').val();
     e = $('#email').val();
     ph = $('#phone').val();
     usernamee = $('#username').val();

            $.ajax({
                type:"POST",
                url:"/registerp",
                data:{
                   userId:uId,
                   firstName:f,
                   lastName:l,
                   password:p,
                   phone:ph,
                   email:e,
                   username:usernamee
                },
                success:function(data){
                   if(data.success){
                    showNotification({
                        message: "Profile saved successfully.",
                        autoClose: true,
                        type: "success",
                        duration: 3
                    });   
                    }
                    else
                    {
                    showNotification({
                        message: "Failed to save profile.",
                        autoClose: true,
                        type: "error",
                        duration: 3
                    });   
                    }
                }
            });
    }

All the info i'm retracting was also put on the page from the session . When i call this POST the req.session is reset to nothing and all the data vanishes.

httponly=false

other pages work fine and some of them have similar functionality, What am i doing wrong here?

I managed to solve it. I knew that a session isn't saved unless the server returns something to the client.

Well, i used a redirect to another page on the same POST. I thought the redirect is a type of GET for the client and that was my mistake. I solved it by returning some random item

res.send({success:true});to that AJAX function and added that if the success = true in the javascript then i perform a GET call from the same operation.

That solved it.

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