你能告诉我如何在我的Angular 2请求中设置'withCredentials = true'吗? 在Angular 1中,它运行良好如下:
x-domain设置和use-credential设置。
var chat = angular.module('chat',[])
//configuration for cord
chat.config(function($httpProvider) {
//Enable cross domain calls
$httpProvider.defaults.useXDomain = true;
//allow credentials
$httpProvider.defaults.withCredentials = true;
});
Angular 1中的函数
//http-structure
this.structureDataRequest = function () {
$http({
method: 'POST',
//graph url
url: 'http://manny.herokuapp.com/audit/get/structure',
header: {'Content-Type': 'application/json'}
}).success(function (response) {
dataReceived = response.content;
buildDiagram.buildDiagramm();
})
};
制作x-domain的Angular 2代码;需要修改以设置withCredentials=true
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable ()
export class StructureRequestService {
result: Object;
constructor (private http: Http) {}
private structureUrl = 'http://manny.herokuapp.com/audit/get/structure'; // URL to structure APi
sendRequest() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
return this.http.post(this.structureUrl,options)
.map((res: Response) => res.json())
.subscribe(res => {this.result = res;
console.log(this.result)});
}
}
备用版本(仍无效)
import { Injectable } from '@angular/core';
import { Http, Response,Headers, RequestOptions } from '@angular/http';
import { Observable } from 'rxjs/Observable';
@Injectable ()
export class StructureRequestService {
result: Object;
constructor (private http: Http) {}
private myUrl = 'http://manny.herokuapp.com/audit/get/structure'; // URL to structure APi
sendRequest() {
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({
headers: headers,
withCredentials: true
});
return this.http.post(this.myUrl,options)
.map((res: Response) => res.json())
.subscribe(res => {this.result = res; console.log(this.result)});
}
}
答案 0 :(得分:0)
您可以登记Angular 2 docs - RequestOptions类型包含withCredentials
字段,该字段应在您的exapmle中设置为true
。不幸的是,我无法在那里看到useXDomain
参数。