Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6969059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:31:58+00:00 2026-05-27T16:31:58+00:00

I am trying to use session with expressjs and jquery. I have a node.js

  • 0

I am trying to use session with expressjs and jquery.

I have a node.js server @127.0.0.1:6060 with this code :

var Express = require('express'),
  App = Express.createServer(),
  Mongoose = require('mongoose'),
  Schema = Mongoose.Schema,
  ObjectId = Schema.ObjectId;

App.configure(function () {
  App.use(Express.bodyParser());
  App.use(Express.methodOverride());
  App.use(Express.logger({ format: '[:remote-addr] :method :url :response-time ms' }));

  App.use(Express.cookieParser());
  App.use(Express.session({ secret: "keyboard cat" }));

  // CORS
  App.use(function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Methods', 'OPTIONS,GET,POST,PUT,DELETE');
    res.header('Access-Control-Allow-Headers', 'Content-Type, X-Requested-With');

    next(); 
  });
});

App.all('/api/users/test1', function (req, res) {
  req.session.test = 'toto';
  console.log(req.session.id)
  res.send({'r': req.session.id});
});
App.all('/api/users/test2', function (req, res) {
  console.log(req.session.id)
  res.send({'r': req.session.test});
});

App.listen(6060, '127.0.0.1');
console.log('App launched ...');

Ok, now if I directly enter http://127.0.0.1:6060/api/users/test1 then http://127.0.0.1:6060/api/users/test2 in my browser, I get my object result with ‘toto’.

However, if I try with jquery 1.7.1 :

$.getJSON('http://127.0.0.1:6060/api/users/test1', function (json) {
    console.log(json.result);
    $.getJSON('http://127.0.0.1:6060/api/users/test2', function (json) {
        console.log(json.result)
    });
});

I get an empty js object in the last print.
I really do not understand. Any idea?

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T16:31:59+00:00Added an answer on May 27, 2026 at 4:31 pm

    Explications

    $.getJSON call to Express to create a new session because your link is absolute (http://…), you need to call your server with a relative link (/api/users/test1 for exemple).

    Solution

    You need to have your application on the same server and port or use an API key to recognize the user.

    Code

    I’ll use the first solution, to have the application on the same server and port.
    On the server, you just need to specify to Express that you use a static directory like this in your App.configure :

    App.use(Express.static(__dirname + '/public/'));
    

    In your public/ folder, put your index.html which contains :

    $.get('/api/users/test1', function (json) {
        // display something like "74V5zXFyOe0KYKG8somXUzoR.maraL4zDfFfyLfEU4W/i8eioMMK5f4+YOnUVeJO8abU"
        console.log(json.r);
        $.getJSON('/api/users/test2', function (json) {
            // display : 'toto'
            console.log(json.r)
        });
    });
    

    Go on http://localhost:6060/ and look the console.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use PHP session without using cookies. I have enabled session.use_trans_sid
I am trying to use it for Login page. if (Session[UserID] == null) Server.Transfer(/Account/Login,
I'm having a problem with Express under Node.js. I'm trying to use sessions to
I am trying to use session state in MVC and I have been stuck
I'm trying to use session[:user.return_to] but with no success... My code: def after_sign_in_path_for(resource) (session[:user.return_to].nil?)
How to get Hibernate session inside a Hibernate Interceptor? I'm trying to use Hibernate
I'm trying use self-signed certificate (c#): X509Certificate2 cert = new X509Certificate2( Server.MapPath(~/App_Data/myhost.pfx), pass); on
I am trying to use session variable( $_SESSION['asc_id'] , which holds some value like
I am trying to make use of session data in my application and for
I'm currently trying to use the current session of a php web page from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.