I am making a PhoneGap-based iPhone app which will connect to a Rails backend. I'm using jQuery to create an Ajax request to Rails server. I'm successfully getting a server response, but I'm having problems with getting cookies and managing sessions.
By default, Rails sends a Set-cookie header when responding to an HTML request. This doesn't 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's is what I have in my ApplicationController (I am using 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 I am using to make the 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){
}
});
});
I haven't come up with a clear answer via Googling, so hopefully you can help. I found this, but didn't find it too helpful:
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/66751d8e54f2fee6