当然可以。
离子应用程序只是发送一个 ajax 请求,cookie 工作好或不依赖于服务器。
我使用过 python Django 服务器和节点服务器,两个 cookie 都运行得很好
下面的节点代码
app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", '*');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials",true);
next();
});
休息api
router.get('/setCookies', function(req, res, next) {
var now = new Date();
var nextYear=new Date(now.setFullYear(now.getFullYear()+1));
//you can change the cookie key and value by your self here
res.cookie('cookiesTest', 'set cookies success,your cookies can be set by server', { expires: nextYear, httpOnly: true });
res.status(200)
res.end('SET COOKIES SUCCESS')
});
router.get('/getCookies', function(req, res, next) {
res.status(200)
res.end(JSON.stringify(req.cookies))
});
离子应用程序代码
var server='http://[YOUR IP HERE]:3000'
$scope.setCookies=function() {
$http({
url: server + '/setCookies',
method: 'GET'
}).success(function (data, header, config, status) {
alert('set cookies success,look console')
$scope.setCookiesStatu=false
console.log(data)
$scope.cookiesValue=data
}).error(function (data, header, config, status) {
alert('set cookies error,check console or your server address is wrong')
console.log(data)
});
}
$scope.getCookies=function() {
$http({
url: server + '/getCookies',
method: 'GET'
}).success(function (data, header, config, status) {
alert('get cookies success,look console')
console.log(data)
$scope.cookiesValue=data
}).error(function (data, header, config, status) {
alert('get cookies error,check console or your server address is wrong')
console.log(data)
});
}
你可以在这里查看我的源代码