2

I am trying to create a session from JQuery to my Sinatra backend. When I send a POST request outside of Ajax, using something like POSTman, I am able to set a session, but when I send via JQuery, I just get NULL in return. How do I create a session using AJAX?Should I be actually doing this via AJAX or should I jus have a login screen?

This is my session (and some other pertinent) setting code:

set :protection, :except => [:http_origin, :remote_token] //I put this because Sinatra was denying the request before
set :session_secret, "My session secret"

post '/session' do
    session[:user_id] ||= 'hihiiddasdah'
    session[:user_id].to_json
end

delete '/session' do
    session.clear
    session[:user_id].to_json

end

get '/session' do
    session[:user_id].to_json
end

and this is my JQuery code for getting the session:

$.post(URL + 'session', {function(data) {console.log(data);});
4

0 回答 0