0

I'm trying to log into a Sinatra app with jQuery, but Sinatra is just not seeming to recognise it at all for some reason, here is my code to see if the user is logged in:

Sinatra app:

get '/logged_in' do
 if session[:logged_in] == true
 status 200
 "OK"
 else
 status 200
 "False"
 end
end

When I browse to /logged_in in my browser, I see OK, but when I execute the following Javascript, I get False printed to the console - seems bizarre to me.

var r = $.ajax({
 type: "GET",
 url: "http://xxx-xxx.com/logged_in"
});
r.success(function(msg) {
 console.log(msg);
});

Any insight would be appreicated!

asked Nov 7, 2012 at 5:42
1
  • have you tried making the success function part of the ajax function setup? i.e. success: function(msg){ ? Like the code here: api.jquery.com/jQuery.ajax/#callback-functions Commented Nov 7, 2012 at 10:53

2 Answers 2

1

It would be good if you could give more details. (Browser, chronological request log including HTTP status codes, or even a dump from wireshark.) However here is my guess:

var r = $.ajax({
 cache: false,
 type: "GET",
 url: "/logged_in",
 success: function(msg) {
 console.log(msg);
 }
});

Some browsers aggressively cache AJAX requests, in particular GET requests. So you may want to try cache: false. Adding the success function later also seems odd to me, I have never seen that in any code.

Edit: Is the server answering the AJAX request on the same domain? (The domain must be the same as in your browser test.) Usually it should not be necessary to specify the domain inside the URL, so I removed it in the code here. So after all it might be a problem due to the Same Origin Policy.

answered Nov 7, 2012 at 13:50
Sign up to request clarification or add additional context in comments.

2 Comments

It's not on the same domain, but I've set the correct Access-Control-Allow-Origin in the server side header. Could this still be the issue?
Don't worry - it was because of cross domain cookies. I should have realised this.
0

I would place my bet on this being a jQuery issue rather than a Sinatra issue. Check that the cookie is being sent for the ajax request. There are a number of reasons why the cookie is not getting sent.

answered Nov 7, 2012 at 10:32

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.