0

i have strange issue in angualr4 http post request ,that is when i use full backend server api URL 'http://localhost:9090/api/login' post request did not make. but when i use simply '/api/login'. post request made with 'http://localhost:4200/api/login' .below is my code

authentication.service.ts

authenticate(model) {

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

            );
}
Sundhar
  • 449
  • 1
  • 5
  • 9
  • Could you give us the log of `console.log(this.endpoint);` ? –  Jul 11 '17 at 09:28
  • Probably you have CORS issue, because your app and your endpoint are on different ports! – Yordan Nikolov Jul 11 '17 at 09:29
  • Possible duplicate of [How to create cross-domain request (Angular 2)?](https://stackoverflow.com/questions/34790051/how-to-create-cross-domain-request-angular-2) – eko Jul 11 '17 at 09:42
  • http://localhost:9090. i did not see any request is made in network tab – Sundhar Jul 11 '17 at 09:43
  • Welcome friends , i think there is no CORS issue. it may be something trivial one. i spend whole day. i did not get any clue to solve this issue.. GET request are working fine @trichetriche end point is http://localhost:9090 – Sundhar Jul 11 '17 at 10:02
  • Thank you, and I assume that http is the default HTTTP service from Angular, not a custom made one ? –  Jul 11 '17 at 10:10
  • If you don't see any requests in your network tab, then you are not calling the method ! you should at least get an error or pending request if you call the wrong endpoint. But the lack of requests means that you're not calling the function from your component, and thus, not the one in your service. –  Jul 11 '17 at 10:12
  • @trichetriche , im using the same method for both end point '/api/login' & 'http://localhost:9090' . for '/api/login' i see request call in network tab but not for other one. thats the strange problem. im very confused of this strange issue – Sundhar Jul 11 '17 at 10:18
  • 1
    As I told you, if you don't see the request in the Network tab, then you're not calling it. But maybe some more code would help us understand ! –  Jul 11 '17 at 10:19
  • i added alert in handleError method its throw alert message – Sundhar Jul 11 '17 at 10:25

1 Answers1

0

It must be the CROS issue. Check your console log for details.

If you are using Chrome try adding --disable-web-security in chrome shortcut.

Disble Web Security