0

Below is the API that basically stores data in session if not already present then returns an empty JSON, otherwise sends the session data stored corresponding to that mail:

app.use('/session', function(req, res) {
  if (req.body.email in req.session) {
    console.log("user data already available");
    res.send(req.session[req.body.email]);
  } else {
    req.session[req.body.email] = {};
    req.session[req.body.email]["email"] = req.body.email;
    req.session[req.body.email]["gender"] = req.body.gender;
    req.session[req.body.email]["dbname"] = req.body.dbname;
    console.log("user session stored");
    res.send({});
  }
});

Below is the ajax call that I am making from my application. It is a cross domain call and I have set the response header accordingly.

$.ajax({
    url: 'url/session',
    type: "POST",
    crossOrigin: true,
    data: {
      "email": "email",
      "gender": "all",
      "dbname": "dbname"
    },
    success: function(data) {
      console.log("inside session api call success");
      console.log(data)
    }
    });
});

Now the problem is that if I hit the API using postman, it works fine but when I use that in my application, it overwrites the session every time and returns empty json. What am I doing wrong?

edit : Figured out that each time ajax call is happening, past session data gets deleted for all emails ids.session value gets reset but still unable to figure out why this is happening.

5
  • 1
    Just add dataType : 'json' on ajax call and try Commented Dec 5, 2017 at 16:46
  • @SantoshSuryawanshi tried it but still the same problem. Commented Dec 5, 2017 at 16:51
  • @SrijanSharma why don't you use an IDE? A breakpoint could answer this question for you immediately. In this day and age not using an IDE, especially with all of the lightweight options available for node is frankly stupid or uninformed. I suggest vs code since you don't have to do anything to set up the node debugger except press F5 and pick node Commented Dec 5, 2017 at 16:54
  • your ajax call URL parameter url: 'url/session', is different form app.use('/session',()) function. just make them equal and see Commented Dec 5, 2017 at 17:03
  • @SantoshSuryawanshi I don't understand what you are trying to say. url basically species the host url on which my api is hosted. What do you mean by make them equal? Commented Dec 5, 2017 at 17:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.