使用cookie保存登录信息,但对于android平台,它无法正常工作

时间:2015-10-05 14:58:39

标签: android angularjs cookies onsen-ui monaca

我使用onsen-ui和angularjs来实现Monaca的登录功能,实际上当我在iphone上调试并在网站上预览时效果很好但是当我尝试在Android系统上使用它时,我发现cookie是没有保存。下次我访问登录页面时,我看到的是欢迎页面,但在android系统上,系统要求我再次登录。 当我登录成功时,我将我的信息保存在cookie中。 我的代码如下:

$scope.login = function(){  
    $.ajax({
        type: 'post',
        url: 'https://www.xxxxxxx.com/api/public/user_login',
        data:{
            username:document.getElementById('login_username').value, 
            password:document.getElementById('login_password').value
        },
        success: function(data) {
            if(data.error != "" & data.error != null ){
                alert(data.error); 
                delCookie('username');                  
            }else if(data.status == 0){
                setCookie('username',data.username,365);
                $scope.username = getCookie('username');
                document.getElementById('loginInfo').style.display="none";
                document.getElementById('welcome').style.display="block";
            }
        },
    });
};
function getCookie(login_name){
if (document.cookie.length>0){ 
    login_start=document.cookie.indexOf(login_name + "=")
    if (login_start!=-1){ 
        login_start=login_start + login_name.length+1; 
        login_end=document.cookie.indexOf(";",login_start);
        if (login_end==-1){
            login_end=document.cookie.length;
        }
        return unescape(document.cookie.substring(login_start,login_end));
    } 
}
return "";
}

function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}

function  delCookie(name){
var  exp  =  new  Date();
exp.setTime  (exp.getTime()  -  1);
var  cval  =  getCookie (name);
document.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString();
}

1 个答案:

答案 0 :(得分:0)

您可以使用现在似乎已弃用的$cookieStore,而不是`'$ cookie是我们需要使用的。

而不是使用document.cookie,这是一个Web API,使用角度内置服务,你不会在android和ios中遇到这个问题。

您需要在控制器中注入依赖项:

为:

angular.module('sampleApp', ['ngCookies'])
.controller('AuthController', function ($scope,
                                        $cookies,//injecting the dependency here
                                        $timeout,
                                        ngDialog) {


$scope.login = function(){  
    $.ajax({
        type: 'post',
        url: 'https://www.xxxxxxx.com/api/public/user_login',
        data:{
            username:document.getElementById('login_username').value, 
            password:document.getElementById('login_password').value
        },
        success: function(data) {
            if(data.error != "" & data.error != null ){
                alert(data.error); 
                delCookie('username');                  
            }else if(data.status == 0){
                setCookie('username',data.username,365);
                $scope.username = getCookie('username');
                document.getElementById('loginInfo').style.display="none";
                document.getElementById('welcome').style.display="block";
            }
        },
    });
};
function getCookie(login_name){
if (document.cookie.length>0){ 
    login_start= document.cookie.indexOf(login_name + "=")
    //use $cookies.get(login_name) to get value of the login_name param set in cookie
    if (login_start!=-1){ 
        login_start=login_start + login_name.length+1; 
        login_end=document.cookie.indexOf(";",login_start);
       //use $cookies.get(login_name) to get value of the login_name param set in cookie
        if (login_end==-1){
            login_end=document.cookie.length;
            //use $cookies.get(login_name) to get value of the login_name param set in cookie
        }
        return unescape(document.cookie.substring(login_start,login_end));
    } 
}
return "";
}

function setCookie(c_name,value,expiredays){
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
//use $cookies.put(key, value) to get value of the login_name param set in cookie
}

function  delCookie(name){
var  exp  =  new  Date();
exp.setTime  (exp.getTime()  -  1);
var  cval  =  getCookie (name);
document.cookie  =  name  +  "="  +  cval  +  ";  expires="+  exp.toGMTString();
//use $cookies.put(key, value) to get value of the login_name param set in cookie
}


}

易于实施和使用,请参考Angular documentation