本文介绍了Phonegap App使用身份验证与Rails 3应用程序通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我可以通过ajax在我的iOS应用程序中成功登录,但后来的电话会给我一个未经授权。似乎会在我的iOS应用程式中设定会话Cookie。



如何让我的iOS应用程式识别我的rails已验证的工作阶段?

解决方案

答案是两个。



首先我必须将它添加到我的ajax

  xhrFields:{
withCredentials:true
}

$>

  $。ajax({
url:....,
type:GET,
xhrFields:{
withCredentials:true
},
complete:hideLoader,
error:function(xhr,txt,err){
//你不能回来了
window.location.hash =;
},
success:function(data,status,res){
window.location.hash =;
}
}

第二,我必须在Rails应用程序中将它添加到我的应用程序控制器:

  def protect_against_forgery? 
unless request.format.json?
super
end
end


I am trying to write an iOS app using Phonegap to communicate with my Rails3 app. I cannot figure out how to handle authentication. I am using Devise/Warden in my Rails app.

I am able to login successfully in my iOS app via ajax but then subsequent calls are giving me a "Unauthorized". It appears that the session cookie isn't getting set in my iOS app.

How do I keep my iOS app aware of my rails authenticated session?

解决方案

The answer was two fold.

First I had to add this to my ajax requests in the iOS app:

xhrFields: {
  withCredentials: true
}

as in...

$.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 = "";
    }
});

Second I had to add this to my Application Controller in the Rails app:

def protect_against_forgery?
  unless request.format.json?
    super
  end
end

这篇关于Phonegap App使用身份验证与Rails 3应用程序通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 18:26