0

我使用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();
}
4

1 回答 1

0

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

与其使用document.cookieweb API,不如使用 angular 内置服务,在 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 文档

于 2015-10-06T06:23:30.010 回答