如何从离子的服务器获取数据

时间:2017-03-15 22:10:10

标签: typescript ionic-framework ionic2

我在离子地狱中离我不远。下面是我当前的代码,它返回一个observable。我需要它来从服务器返回响应(它在一个单独的文件中用于分离关注点)

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();
}

home.ts

constructor(public events: Events, public http: Http,
    ...

smsPressed(item){
    console.log(getToken(this.http));
}

错误

Error in ./HomePage class HomePage - caused by: __webpack_require__.i(...) is not a function

1 个答案:

答案 0 :(得分:1)

Http.post未返回Promise,而是返回Observable

auth.ts

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){
    getToken(this.http).subscribe(token => {
         console.log(token);   
    })
}