【发布时间】:2018-07-10 08:46:45
【问题描述】:
所以基本上我是在向服务器请求一个 post 请求,作为回报,以这种形式在 JSON 对象中发送一个令牌:
{
token: "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIyLCJpc3MiOiJodHRwczovL2hvYnVkZGllcy5jb20vYXBpL2F1dGgvbG9naW5fYXBpIiwiaWF0IjoxNTE3MzkwNTkwLCJleHAiOjE1MTczOTQxOTAsIm5iZiI6MTUxNzM5MDU5MCwianRpIjoiWUxtdXJ5TkV2UzlwSEExTyJ9.nWV7OWLYdMJmZCHNP9tFuAmZ84DwzYO00O3jQ_RfhXQ"
}
我需要的是将该令牌附加到 apiUrl 以获取当前登录用户的适当信息。 服务器在变量“result”中返回这个 JSON 对象,如下所示:
// for login the user in the app.
loginUser() {
//values from the login form
var data = {
email: this.emailVar,
password: this.passwordVar
}
//calling the loginUser method from the Provider: Test.ts
this.restProvider.loginUser(data)
.then((result) => {
//storing the returned Token from the server.
var login_token = result;
//storing the retured Token into Ionic Storage.
this.storage.set("tokenn", result);
console.log(result);
console.log('success');
//if successful, then set the rootPage to TabsPage
this.navCtrl.setRoot(TabsPage);
}, (err) => {
console.log(err);
});
但是当我附加结果时,存储在存储中,它附加了返回的对象,这里我只需要字符串。
我只需要这个:
“eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIyLCJpc3MiOiJodHRwczovL2hvYnVkZGllcy5jb20vYXBpL2F1dGgvbG9naW5fYXBpIiwiaWF0IjoxNTE3MzkwNTkwLCJleHAiOjE1MTczOTQxOTAsIm5iZiI6MTUxNzM5MDU5MCwianRpIjoiWUxtdXJ5TkV2UzlwSEExTyJ9.nWV7OWLYdMJmZCHNP9tFuAmZ84DwzYO00O3jQ_RfhXQ” EM> 强>
那么,如何从我的 Ionic 应用程序中的对象中提取该字符串。
【问题讨论】:
-
我认为你必须做 result['token']
标签: angularjs json ionic-framework ionic2 ionic3