Typescript 如何在ionic中从服务器获取数据
在爱奥尼亚的地狱里,我已经走出了我的深渊。下面是我当前的代码,它返回一个可观察的。我需要它从服务器返回响应(出于分离关注点的目的,它位于单独的文件中) auth.tsTypescript 如何在ionic中从服务器获取数据,typescript,ionic-framework,ionic2,Typescript,Ionic Framework,Ionic2,在爱奥尼亚的地狱里,我已经走出了我的深渊。下面是我当前的代码,它返回一个可观察的。我需要它从服务器返回响应(出于分离关注点的目的,它位于单独的文件中) auth.ts import { Http } from '@angular/http'; import 'rxjs/add/operator/map'; export const getToken = function(http){ let response = http.post('localhost:3000/auth/authe
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
export const getToken = function(http){
let response = http.post('localhost:3000/auth/authenticate',
{email: 'mendelh1537@gmail.com', password: 'password'}).then();
return response();
}
export const getToken = function(http){
return http.post('localhost:3000/auth/authenticate',
{email: 'mendelh1537@gmail.com', password: 'password'});
}
home.ts
constructor(public events: Events, public http: Http,
...
smsPressed(item){
console.log(getToken(this.http));
}
constructor(public events: Events, public http: Http,
...
smsPressed(item){
getToken(this.http).subscribe(token => {
console.log(token);
})
}
错误
Http.post
不返回Promise
它返回可观察的
auth.ts
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
export const getToken = function(http){
let response = http.post('localhost:3000/auth/authenticate',
{email: 'mendelh1537@gmail.com', password: 'password'}).then();
return response();
}
export const getToken = function(http){
return http.post('localhost:3000/auth/authenticate',
{email: 'mendelh1537@gmail.com', password: 'password'});
}
home.ts
constructor(public events: Events, public http: Http,
...
smsPressed(item){
console.log(getToken(this.http));
}
constructor(public events: Events, public http: Http,
...
smsPressed(item){
getToken(this.http).subscribe(token => {
console.log(token);
})
}