2017-07-11 5 views
0

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") 

      ); 
} 
+0

Können Sie uns geben, das Protokoll der 'console.log (this.endpoint);'? – trichetriche

+0

Wahrscheinlich haben Sie ein CORS-Problem, weil Ihre App und Ihr Endpunkt sich auf verschiedenen Ports befinden! –

+0

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

Antwort

0

Es muss das CROS-Problem sein. Überprüfen Sie Ihr Konsolenprotokoll auf Details.

Wenn Sie Chrome verwenden, versuchen Sie, --disable-web-security in Chrome Shortcut hinzuzufügen.

Disble Web Security

+0

im Netzwerk auslöst. Ich habe keine Anfrage gesehen, dass ein Anruf getätigt wird – Sundhar