How to include cookies in JSON response in Rails?

I am creating a PhoneGap-based iPhone application that will connect to the Rails backend. I am using jQuery to create an Ajax request to a Rails server. I am successfully receiving a server response, but I am having problems getting cookies and session management.

By default, Rails sends a Set-cookie header when responding to an HTML request. This does not seem to happen when Rails sends a JSON response to an Ajax request.

Is there a way to send a Set-cookie header when an Ajax request is made to a Rails server?

Here is what I have in my ApplicationController (I use Devise for Authentication):

def sign_in_and_redirect(resource_or_scope, resource=nil)
scope      = Devise::Mapping.find_scope!(resource_or_scope)
resource ||= resource_or_scope
sign_in(scope, resource) unless warden.user(scope) == resource
respond_to do |format|
   format.html {redirect_to stored_location_for(scope) || after_sign_in_path_for(resource) }
   format.json {render :json => { :success => true, :session_id => request.session_options[:id], :current_user => current_user} }
end

Here is the jQuery function that I use to make an Ajax request

$('#user_submit').click(function(){
    $.ajax({beforeSend: function(xhrObj){ xhrObj.setRequestHeader("Accept","application/json");},
      type: 'POST',
      url: 'http://localhost:3000/users/sign_in',
      data: "{'user':{'remember_me':'0','password':'pass1word','email':'email@company.com'}}",
      contentType: "application/json",
      dataType: "application/json",
      complete: function(data, textStatus){
      }
    });
 });

Googling, , , . , :

http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/66751d8e54f2fee6

+5
2

, Rails Set-cookie Ajax JSON. , Safari cookie, . cookie, Safari " " "" iPhone- HTML Safari.

Phonegap , :

http://groups.google.com/group/phonegap/browse_thread/thread/3290c5ac4e05be69?fwc=1 http://stackoverflow.com/questions/3709315

0

, , . curl , "Accept: text/html", "Accept: application/json" Set-Cookie . , .

:

" HTTP-
, cookie ( Set-Cookie Set-Cookie2 Cookie ), .

, , , . cookie, .

0

All Articles