withCredentials:在带有angular2

时间:2017-10-04 20:28:47

标签: javascript angular firefox

我有一项服务试图在angular2中使用 withCredentials:true 标记发出HTTP请求,以便我可以发送用户工作站凭据而不是手动输入但是它在最新版Chrome和IE11中运行良好但是没有使用Firefox 54.0.1(32位)。

如何解决这个问题?任何人都可以帮助我。

reaction.service.ts

import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

@Injectable()

export class ReactionService {
    constructor(private http: Http) { }

    getReaction(ruid) {
        const headers = new Headers();
        headers.append('Content-Type', 'application/x-www-form-urlencoded');
        headers.append('Accept', 'application/json');
        const body = 'ruid=' + ruid;
        const options = new RequestOptions({ headers: headers, withCredentials: true });
        return this.http.post('https://serviceUrl', body, options).map(
            (response: Response) => {
                const data = response.json();
                console.log(data);
                 return data;
            }
        ).catch(
            (error: Response) => {
                console.log(error);
                return Observable.throw(error);
            }
            );
    }
}
reaction.component.ts

import { Component, OnInit, OnDestroy, AfterViewInit, Output, EventEmitter } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Http, RequestOptions } from '@angular/http';
import { Route } from '@angular/router';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-reaction',
  templateUrl: './reaction.component.html',
  styleUrls: ['./reaction.component.scss']
})
export class ReactionComponent implements OnInit, OnDestroy, AfterViewInit {
  
  ruid: any = '4b2f4598cecf4ca1bd5d5aa3db34d6a6';
 
  constructor(private reactionService: ReactionService) {
    
  }

  ngAfterViewInit() {
    this.reactionService.getReaction(this.ruid).subscribe(
      (data: any) => {
        console.log(data);
      },
      (error) => {
        console.log(error);
      }
    );
  }

  ngOnInit() {
 
  }

}

0 个答案:

没有答案