Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 使用头授权获取数据2_Ruby On Rails_Angular_Ionic2 - Fatal编程技术网

Ruby on rails 使用头授权获取数据2

Ruby on rails 使用头授权获取数据2,ruby-on-rails,angular,ionic2,Ruby On Rails,Angular,Ionic2,我目前正在使用RubyonRails作为后端开发离子2。我面临的问题是,我很难理解可观察的事物和承诺。他们之间有关系吗?现在,我正试图在POST请求通过头部身份验证后检索数据 //clocks.ts (Provider) import { Injectable } from '@angular/core'; import { Http, Headers, Response, RequestOptions } from '@angular/http'; import { Storage } fr

我目前正在使用RubyonRails作为后端开发离子2。我面临的问题是,我很难理解可观察的事物和承诺。他们之间有关系吗?现在,我正试图在POST请求通过头部身份验证后检索数据

//clocks.ts (Provider)

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import { Storage } from '@ionic/storage';
import 'rxjs/add/operator/map';

@Injectable()
export class Clocks {

  baseUrl: string = "http://localhost:3000/api/v1"
  token: any;

  constructor(public http: Http, public storage: Storage) {}

  getAttendanceInfo() {
    return new Promise((resolve,reject) => {

      // Load token
      this.storage.get('token').then((value) => {
        this.token = value;

        let headers = new Headers();
        headers.append('Authorization', 'Token ' + this.token);
        this.http.get(this.baseUrl + '/attendances.json', {headers: headers})
          .subscribe(res => {
            resolve(res);
          }, (err) => {
            reject(err);
          })
        });
      });
  }
在出席页面

//attendance.ts (Page)

loadAttendance() {
  this.clocks.getAttendanceInfo().then(res => {
    let response = (<Response>res).json();
    this.attendance = response.data;
    console.log(this.attendance)
  })
}
//attention.ts(第页)
出席人数(){
this.clocks.getAttendanceInfo().then(res=>{
let response=(res.json();
this.attention=response.data;
console.log(this.attention)
})
}
以下是我的问题

  • 在这种情况下,我可以使用Observables获得与getAttendanceInfo()方法相同的结果吗?它们是如何工作的

  • 还有,是否有任何方法可以从存储器中检索每个页面请求的令牌,而无需为标题重写相同的代码?例如,一种方法可以始终用于从存储器中检索令牌并在标头处追加

  • //clocks.ts (Provider)
    
    import { Injectable } from '@angular/core';
    import { Http, Headers, Response, RequestOptions } from '@angular/http';
    import { Storage } from '@ionic/storage';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    export class Clocks {
    
      baseUrl: string = "http://localhost:3000/api/v1"
      token: any;
    
      constructor(public http: Http, public storage: Storage) {}
    
      getAttendanceInfo() {
        return new Promise((resolve,reject) => {
    
          // Load token
          this.storage.get('token').then((value) => {
            this.token = value;
    
            let headers = new Headers();
            headers.append('Authorization', 'Token ' + this.token);
            this.http.get(this.baseUrl + '/attendances.json', {headers: headers})
              .subscribe(res => {
                resolve(res);
              }, (err) => {
                reject(err);
              })
            });
          });
      }
    

    非常感谢你们能消除我的困惑。

    我找到了解决办法

    您可以作为服务提供商的一部分创建身份验证。对于这种情况,我使用的是本地存储。对于备注,令牌的结构也取决于您的后端。在我的例子中,我使用了来自Rails的带有http令牌的身份验证方法,其结构如下

    GET /attendances HTTP/1.1
    Host: localhost:3000
    Authorization: Token token=123123123
    
    我们必须与之相匹配

    // ../providers/auth.ts
    
    import { Injectable } from '@angular/core'; 
    import { Http, Headers } from '@angular/http';
    import 'rxjs/add/operator/map';
    
    @Injectable()
    export class Auth {
    
      constructor(public http: Http) {}
    
      createAuthorizationHeader(headers: Headers) {
        headers.append('Authorization', 'Token ' + window.localStorage.getItem('token'));
      }
    }
    
    如果您返回一个http请求,它将以可观察的格式返回。除非你想兑现承诺,否则你不必为此做任何事

    // ../pages/attendances/attendances.ts
    
    import { Component } from '@angular/core';
    
    import { NavController } from 'ionic-angular';
    import { Auth } from '../../providers/auth';
    
    @Component({
      selector: 'page-home',
      templateUrl: 'home.html'
    })
    
    const baseUrl: string = 'http://localhost:3000/api/v1/';
    
    export class HomePage {
    
      constructor(public navCtrl: NavController, public auth: Auth) {}
    
      ionViewDidLoad() {
        this.getAttendances();
      }
    
      getAttendances() {
        return this.http.get(baseUrl + 'bookings.json', { headers: headers })
          .map(data => {
            // convert your data from string to json
            data = data.json();
          })
          .subscribe(response => {
            // to subscribe to the stream for the response
            console.log(response);
            // you can save your response in object based on how you want it
          })
      }