我有一种情况,我需要从Ionic 2应用程序中的存储中获取一段数据,然后使用该数据创建HTTP请求.我遇到的问题是SqlStorage方法返回promises,http meth返回一个observables.我必须做这样的事情才能让它发挥作用:
getToken() {
return this.storage.get('token').then((token) => {
this.token = token;
return token;
});
}
loadStuff(){
return this.tokenService.getToken().then(token => {
return this.http.get("https://www.urltodatasource.com/api/v1/Endpoint?access_token="+token).map(res => res.json());
});
}
Run Code Online (Sandbox Code Playgroud)
然后做这样的事情让它工作:
this.tokenService.loadStuff().then(observable => {
observable.subscribe(data => {
this.storage.set('stuff', data);
return data;
});
})
Run Code Online (Sandbox Code Playgroud)
我对Angular和Ionic一般都是新手,所以我觉得有更好的方法来完成我想要做的事情,但我只是不知道如何做.此外,关于可观察量的所有可用资源都非常快速地变得非常复杂,这使得像我这样容易受到影响的年轻开发人员非常困惑.
任何人都可以更好地了解如何做到这一点吗?谢谢!
长期读者,第一次海报。我对 jQuery 和 JSON 的世界非常陌生,并且一直看到我正在运行的登录脚本存在问题。
最终目标是从表单中捕获数据,将该数据传递到 PHP 文件以通过 jQuery.ajax() post 进行处理,将数据与 MySQL 数据库进行比较以进行身份验证,并返回数据是否成功。
我的问题是我无法将 JSON 格式的数据从 PHP 脚本传递回 jQuery。使用 Chrome 的开发人员工具查看处理时,我看到“登录失败”。我通过将数组 $rows 扔到我的 error_log 文件中对其进行了双重检查,它返回格式正确的 JSON,但我终生无法将它返回到 jQuery 文件。任何帮助表示赞赏。
我的表单输入:
<!-- BEGIN: Login Page -->
<section data-role="page" id="login">
<header data-role="header" data-theme="b">
<a href="#landing" class="ui-btn-left">Back</a>
<h1>Please Log In</h1>
</header>
<div data-role="content" class="content" data-theme="b">
<form id="loginForm" action="services.php" method="post">
<div data-role="fieldcontain">
<label for="schoolID">School ID</label>
<input type="text" name="schoolID" id="schoolID" value="" />
<label for="userName">Username</label>
<input type="text" name="userName" id="userName" value="" />
<label for="password">Password</label>
<input type="password" name="password" id="password" …Run Code Online (Sandbox Code Playgroud)