我使用 ionic 和 angularjs 进行凭据验证,我想要的是当用户已经登录然后转到成员页面,当有 http post/get/else 请求时,我想添加 http 标头请求,如果不使用我是成功的sqlite 但是当我使用 sqlite(ngCordova 插件)更改我的代码时出现错误。它返回空白,我的 Xcode 日志中没有任何信息。这里的代码
//app.js
'use strict';
var db = null;
angular.module('logiseraApp', ['ionic','ngCordova'])
.run(function($ionicPlatform, $cordovaSQLite) {
$ionicPlatform.ready(function() {
//SQLite database
db = $cordovaSQLite.openDB("my.db", 1);
//create tables
db.transaction(function(tx) {
//create session table
tx.executeSql('CREATE TABLE IF NOT EXISTS session (id integer primary key, name varchar(50), value text)');
}, function(e) {
console.log("ERROR: " + e.message);
});
});
})
//this is my focus error
//i check if session is not empty
//and fetch user data then store to config.headers
//there is no error if i"m not using sqlite
.factory('authInterceptor', function($location, $q, $cordovaSQLite) {
return {
request: function(config) {
config.headers = config.headers || {};
var query = "SELECT value from session where name = ? limit 1";
$cordovaSQLite.execute(db, query, ["userdata"]).then(function(res) {
if(res.rows.length > 0){
console.log("result length", res.rows.length);
var userdata = JSON.parse(res.rows.item(0).value);
//apache
//config.headers.token_user_id = userdata.user_id;
//nginx
config.headers['Token-User-Id'] = userdata.user_id;
}
}, function (err) {
console.error(err);
});
return config;
}
};
})
.config(function($stateProvider, $urlRouterProvider, $httpProvider) {
$httpProvider.interceptors.push('authInterceptor');
});
谢谢,