开发者

creating session keys on javascript requests

so this may have something to do with my JS request being from uploadify, but basically what i want to do is on an uploadify JS request, i want to set a session value. this does not seem to work.

however when i make an HTML request, it does.

i have uploadify passing my session key and value through as a parameter with the request... is there something i need to do with it to authenticate and set the 开发者_C百科session?


Yes, unfortunately its a bit tricky but totally possible.

First you will need a non-cookie store session. If you are using a cookie store session you will need to switch to a database/memcache/redis or similar session store.

How sessions normally work is that rails give you a cookie with a session id. When your browser makes a normal request it sends the cookies to rails, and rails therefore knows immediately which session to look for in its database, finds it, and loads it into session[].

When your using uploadify or similar techniques, the request does not have the cookie data sent with it, therefore rails has no idea who is making the request, and making the whole session stuff not work at all.

So the way to fix it is to have uploadify send the session id as a parameter, which sounds like what you may be doing, but then you need to add some Rack Middleware to the mix.

The Rack Middleware is basically a layer that sits inbetween the initial request and rails, and it will look at the parameters, and if it sees a session_id in the normal http parameters, it will turn this into what looks like a cookie, before it gets to rails. So essentially rails thinks it is a normal user with a normal cookie, then all session stuff starts to work, and rails can know who is making the request.

There is quite a few examples of these online so rather than re-writing I will just link:

http://railstips.org/blog/archives/2009/07/21/uploadify-and-rails23/

http://metautonomo.us/2010/07/09/uploadify-and-rails-3/

Good Luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜