How to access API using Angular 2?

I want to be able to navigate outside of my Angular 2 application on mywebsite.com/api. This will lead me to an API application hosted on the same server.

This is my current route setup.

export const routes: Routes = [ {path: '', redirectTo: '/home', pathMatch: 'full'}, {path: 'home', component: HomeComponent}, {path: 'registration', component: RegistrationComponent}, {path: '**', redirectTo: '/home', pathMatch: 'full'} ]; 

How would I rule out the path since I don't need a component or template for it?

+5
source share
1 answer

In angular2 you will call api using http docs here ,

 import { Http, Response, Headers } from '@angular/http'; export class ProfileComponent implements OnInit { constructor( private router: Router, private auth: Auth, private http: Http ) { } private personalSubmit() { let headers = new Headers(); headers.append('Content-Type', 'application/json'); return this.http .post('http://localhost:4200/api/apiEndpointURL', this.personal, { headers: headers }) .map((res:Response) => res.json()) .subscribe((res)=>{ //do something with the response here console.log(res); }); } 

This is an example of publishing a form. The values ​​are in the form of post , and the answer is console.log to terminal in this case. This is an example of working with the end of node.js. I'm not sure what will need to be changed for php , but in the end you will need an http package . The only thing you may need to change the type of application depending on what you return.

I believe that for php you use this as an application type,

 headers.append('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 

I found ane xample for php using

0
source

Source: https://habr.com/ru/post/1260673/


All Articles