如何在ionic中从服务器获取数据



我在离子地狱中已经脱离了我的深渊。 下面是我当前的代码,它返回一个可观察量。我需要它从服务器返回响应(它位于一个单独的文件中,用于分离关注点(

身份验证

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

首页

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

Http.post 不返回Promise而是返回Observable

身份验证

export const getToken = function(http){
    return http.post('localhost:3000/auth/authenticate', 
                {email: 'mendelh1537@gmail.com', password: 'password'});
}

首页

constructor(public events: Events, public http: Http,
    ...
smsPressed(item){
    getToken(this.http).subscribe(token => {
         console.log(token);   
    })
}

相关内容

  • 没有找到相关文章