我正在製作一個基於PhoneGap的iPhone應用程序,它將連接到Rails後端。我正在使用jQuery爲Rails服務器創建一個Ajax請求。我正在成功獲取服務器響應,但我在獲取Cookie和管理會話方面遇到問題。如何在Rails中的JSON響應中包含Cookie?
默認情況下,Rails在響應HTML請求時發送Set-cookie標頭。當Rails向Ajax請求發送JSON響應時,這似乎不會發生。
當Ajax請求發送到Rails服務器時,有沒有辦法發送Set-Cookie頭?
這裏是我在我的ApplicationController中(我使用設計的驗證):
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
這裏是jQuery的功能,我使用,使Ajax請求
$('#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 protected]'}}",
contentType: "application/json",
dataType: "application/json",
complete: function(data, textStatus){
}
});
});
我的天堂不會通過谷歌搜索得到明確的答案,所以希望你能提供幫助。我發現這一點,但沒有發現它太大的幫助:
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/66751d8e54f2fee6