当然可以。
ionic应用程序仅发送ajax requset,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)
});
}
你可以在这里查看我的源代码