Ich habe seltsames Problem in Angualr4 http Post Anfrage, das ist, wenn ich volle Backend-Server API URL 'http://localhost:9090/api/login' Post Anfrage nicht machen. aber wenn ich einfach '/ api/login' benutze. Post-Anforderung, die mit 'http://localhost:4200/api/login' .below ist mein Codenicht möglich http http Anfrage in angular4 mit Backend-Server
authentication.service.ts
authenticate (Modell) {
console.log('this.globalConstant.apiLoginURL',this.globalConstant.apiLoginURL);
console.log('this.apiLoginURL',this.apiLoginURL);
let headers = new Headers({ 'Content-Type': 'application/json' }); // ... Set content type to JSON
let options = new RequestOptions({ headers: headers }); // Create a request option
let bodyString = JSON.stringify(model); // Stringify payload
console.log('headers==>'+headers);
console.log('options==>'+options);
console.log('bodyString==>'+bodyString);
return this.http.post(this.endpoint+'/api/login', bodyString, options)
.map(this.extractData)
.catch(this.handleError);
}
login.component.ts
login() {
this.newsDataService.authenticate(this.account)
.subscribe(
data => {
this.router.navigate([this.returnUrl]);
},
error => {
this.alertService.error(error);
console.log('alertService==>'+error);
},
() => console.log("Finished")
);
}
Können Sie uns geben, das Protokoll der 'console.log (this.endpoint);'? – trichetriche
Wahrscheinlich haben Sie ein CORS-Problem, weil Ihre App und Ihr Endpunkt sich auf verschiedenen Ports befinden! –
Mögliches Duplikat von [So erstellen Sie eine domänenübergreifende Anforderung (Angular 2)?] (Https://stackoverflow.com/questions/34790051/how-to-create-cross-domain-request-angular-2) – echonax