【发布时间】:2017-09-09 07:52:35
【问题描述】:
我目前正在开发我的第一个 Ionic 2 应用程序,但我还不是很喜欢 typescript..
我想在构造函数中调用 authenticate() 方法,然后再调用
- 将整个 JSON 响应放入 textarea 和/或
- 在我的 HTML 中访问 json 响应的用户名和密码值
打字稿:
export class WelcomePage {
public data: string;
username: string;
password: string;
constructor(public navCtrl: NavController, public http: Http) {
this.authenticate();
}
authenticate() {
var creds = { username: 'user1', password: 'pw1' };
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://www.xyz.api.php', creds, {
headers: headers
})
.map(res => res.json())
.subscribe(
data => this.data,
err => this.logError(err),
() => console.log('Completed')
);
}
}
我已经从 API 得到响应:
{ "Person":[ {"Username":"user1","Password":"pw1"} ] }
HTML:
<textarea>here: {{data}}</textarea>
--> 文本区域为空
【问题讨论】:
-
数据是json对象,所以你需要像这样循环对象
-
可惜还是空的,没有出现textareas
-
在输出屏幕中你得到 [Object Object]
-
根本没有输出
标签: html json api angular typescript