如何使用Angular 2访问API?

mcf*_*esh 5 angular2-routing angular2-services angular

我希望能够在我的Angular 2应用程序之外导航到mywebsite.com/api.这应该带我到同一服务器托管的API应用程序.

这是我目前的路线设置.

export const routes: Routes = [
  {path: '', redirectTo: '/home', pathMatch: 'full'},
  {path: 'home', component: HomeComponent},
  {path: 'registration', component: RegistrationComponent},
  {path: '**', redirectTo: '/home', pathMatch: 'full'}
];
Run Code Online (Sandbox Code Playgroud)

如何排除路径,因为我不需要组件或模板?

wun*_*uno 0

angular2api使用此处的 http 文档进行呼叫,

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);
        });
    }
Run Code Online (Sandbox Code Playgroud)

这是发布表单的示例。在本例中,表格中的值为post,响应console.logterminal。这是一个使用 Node.js 后端的示例。我不确定需要更改什么,php但最终您将需要http package. 唯一的问题是您可能需要根据您返回的内容更改应用程序类型。

我相信php您使用它作为应用程序类型,

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

我找到了 php 使用的示例