Thursday, 13 April 2017

Session-Express not setting in browser using AJAX but Postman request works

I have a node.js (using latest versions for everything) with the following:

var express = require('express');    
var session = require('express-session');
var cookieParser = require('cookie-parser');
var app = express();

app.use(cookieParser());
app.use(session({secret: '123456'}));

app.get("/login/:userName/password/:password",
  function(req, res, next) {
    res.cookie('testCookie', 'test', { path: "/", maxAge: 900000, httpOnly: true, rolling: false });
    req.session.userHash = "test";
    res.status(200).send("Logged In "+req.session.userHash);
});

When I use Postman or just type it in the browser URL, I get a correct response and I can see the cookie stored just fine. However, when I use javascript on another page of my website using AJAX, I get a response back just fine but the cookie is not stored. Here is the js call:

  $.ajax({
        url: 'http://myurl.com:3000/login/'+username+'/password/'+password,
        type:'GET',
        json: true,
        success:function(response){
           alert("Logged In");
        },
        error:function(error){
             alert("Error on login: "+error.status);
        }
    });



via Scott

No comments:

Post a Comment