Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 - http://localhost:4200/ being appended with api call why?

just start learning angular2 followed the heroes tutorial. I am making a create request, the URL is perfectly fine, the parameters are fine. But I am still confused why http://localhost:4200/ is being appended with my API call, and because of that the URL gets totally changed, and the calls failed.please shed some light over this issue. I googled a lot but could find the reason.

My Create Method

 create(user: object): Promise<any> {
    return this.http
        .post('localhost/usmanProject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0', user, { headers: this.headers })
        .toPromise()
        .then(res => res.json().data)
        .catch(this.handleError);
}

enter image description here

like image 750
Usman Iqbal Avatar asked Sep 13 '17 19:09

Usman Iqbal


2 Answers

You need to add your protocol for your URL. Otherwise, it's a relative URL:

.post('http://localhost/usmanProject/api/web/v1/users?access-token=n-EJtZiejtz5RSVWe-U14G4kCnPWMKf0', user, { headers: this.headers })
like image 135
Gosha_Fighten Avatar answered Sep 21 '22 03:09

Gosha_Fighten


Your Url should be like url:string ="www.example.com";

It should not be like url:string =" 'www.example.com' ";

like image 37
SUNIL JADHAV Avatar answered Sep 18 '22 03:09

SUNIL JADHAV