Sessions and AJAX

228 views
Skip to first unread message

Masiar

unread,
Feb 29, 2012, 5:32:00 AM2/29/12
to Express
Hi all, I have a Google Chrome extension that connects to an
express.js server; sessions are working so whenever I open the popup
of my extension if I'm logged in, I can see the logged-in screen. The
problem is that when I do an AJAX request from the background page, it
gets it like it's not logged in. I do the AJAX request in this way
(really simple):

$.ajax({
type: "PUT",
url: "http://localhost:3000/addTransaction",
data: receivedData,
success: function(data){
console.log(data);
}
});

Does anyone know how to have myself authenticated in this AJAX request?

Masiar

unread,
Feb 29, 2012, 5:12:18 PM2/29/12
to Express
After thinking a lot about this, I came up with an explanation of this
strange behavior. The problem is that the login is called from the
popup.html page, while this ajax request is called within the
background.html page. Is there a way to maintain the same session
within two different pages accessing the server? That is, can I base
session on the user instead that on the pages? I know it sounds
strange but it's the only explanation I can get.

Angelo Chen

unread,
Feb 29, 2012, 10:15:26 PM2/29/12
to Express
Hi,

not really sure if I have a solution, my app use jquerymobile which
uses ajax post during submit, I have found out the connect.sid in the
ajax post or ordinary post are the same, that means same session id is
used.

one way to find out is, install the proxy server Charles, and check
the connect.sid there.

hope this helps.

Angelo

Masiar

unread,
Mar 1, 2012, 5:21:24 AM3/1/12
to Express
Thanks, I'll try to! Supposing the two connect.sid being different,
how can I solve my problem? I would like them to be the same...

Angelo Chen

unread,
Mar 1, 2012, 7:18:32 AM3/1/12
to Express
they should be the same.
if your app is running in another computer, make sure two computer has
the same date and time.

Spencer Alger

unread,
Mar 1, 2012, 2:49:49 PM3/1/12
to expre...@googlegroups.com
If the requests are coming from the same client the cookies should be sent in both requests. If the cookies are sent, then the session should be found (assuming it was saved correctly). Like Angelo said, I would check out the network activity and see if the cookies are sent in the headers of both requests. If they are then it's a problem with Express or the way that you are storing user information to the session.
Reply all
Reply to author
Forward
0 new messages