Verwenden von Angular 5 (http://localhost) und HttpClient POST-Anforderung an einen REST-Dienst, gehostet auf einem anderen Server (http://192.168.200.75:5555).Kann HttpHeader während HttpClient POST in Angular nicht ändern 5
folgende Fehler bekommen: -
fehlgeschlagen http://192.168.200.75:5555/rest/pub/ws/provider/requestToCSAF/ laden: Antwort auf Preflight-Anfrage nicht Zugriffskontrolle Prüfung bestehen: Nein "Access-Control-Allow-Origin-Header ist auf der angeforderte Ressource. Ursprung 'http://localhost' ist daher nicht erlaubt.
Service-Klasse sieht aus wie
import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import {Observable} from 'rxjs/Observable';
import {RequestJSON} from './requestJSON';
const httpOptions = {
headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Access-
Control-Allow-Origin': '*' })
};
@Injectable()
export class MyService {
private serviceUrl
= 'http://192.168.200.75:5555/rest/pub/ws/provider/requestToCSAF/';
constructor(private httpClient: HttpClient) {
}
postRequest(json: RequestJSON): Observable<RequestJSON> {
const _options = {headers: new HttpHeaders({'Content-Type': 'application/json', 'Access-Control-Allow-Origin': '*'}) };
return this.httpClient.post(this.serviceUrl, json, httpOptions)
.catch((error: any) => Observable.throw(error.json().error || 'Server error'));
}
}
Aber Request-Header sieht wie folgt und werfen keine "Access-Control-Allow-Origin-Header auf die angeforderte Ressource Fehlermeldung vorliegt: -
OPTIONS rest/pub/ws/provider/requestToCSAF/ HTTP/1.1
Host: 192.168.200.75:5555
Connection: keep-alive
Access-Control-Request-Method: POST
Origin: http://localhost
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
(KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36
Access-Control-Request-Headers: access-control-allow-origin,content-type
Accept: */*
DNT: 1
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Empfohlen: https://angular.io/tutorial/toh-pt6