1

我正在尝试使用 Phonegap 编写一个 iOS 应用程序来与我的 Rails3 应用程序进行通信。我不知道如何处理身份验证。我在我的 Rails 应用程序中使用 Devise/Warden。

我能够通过 ajax 在我的 iOS 应用程序中成功登录,但随后的调用给了我“未经授权”。看来我的 iOS 应用程序中没有设置会话 cookie。

如何让我的 iOS 应用了解我的 Rails 身份验证会话?

4

1 回答 1

1

答案是双重的。

首先,我必须将其添加到 iOS 应用程序中的 ajax 请求中:

xhrFields: {
  withCredentials: true
}

如...

$.ajax({
    url: ....,
    type: "GET",
    xhrFields: {
        withCredentials: true
    },
    complete: hideLoader,
    error: function(xhr,txt,err) {
        // you can't get back anyways
        window.location.hash = "";
    },
    success: function(data,status,res) {
        window.location.hash = "";
    }
}); 

其次,我必须将其添加到 Rails 应用程序中的应用程序控制器中:

def protect_against_forgery?
  unless request.format.json?
    super
  end
end
于 2012-08-10T17:10:19.393 回答