简体   繁体   English

从ajax发布时,nodejs express会话未定义

[英]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 . 我要撤消的所有信息也都放在了session的页面session When i call this POST the req.session is reset to nothing and all the data vanishes. 当我调用此POSTreq.session被重置为req.session ,所有数据均消失。

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. 好吧,我在同一个POST上使用了重定向到另一个页面。 I thought the redirect is a type of GET for the client and that was my mistake. 我认为重定向是客户端的GET类型,这是我的错误。 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. res.send({success:true});到该AJAX函数,并补充说,如果javascript中的success = true,则我从同一操作执行GET调用。

That solved it. 那解决了。

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

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