来自rest api的Angular 2哈希路由策略

时间:2016-07-13 13:18:41

标签: php rest angular angular2-routing angular2-services

路由问题是我使用服务从REST API调用JSON格式的数据。这是建立在PHP方面。并且它完美地工作,它返回了我这样的东西:

    [
  {
    "id": 1,
    "created_by": 1,
    "updated_by": 1,
    "created_at": 0,
    "updated_at": 0,
    "active": 1,
    "name": "MyCompany",
    "currency_id": null,
    "logo_img": null,
    "phone": null,
    "email": null,
    "website": null
  }
]

但是在尝试使用哈希位置策略时,我收到了index.html的响应,而不是来自服务的数据。

http://apache:8080/MyCompany/backend/web/#/base/company

这件事告诉我索引错误:

  

意外的令牌<在位置0的JSON中

因为我的服务试图将响应数据转换为JSON格式(如果没有以JSON格式接收)

boot.ts如下:

import {bootstrap}    from 'angular2/platform/browser';
import {AppComponent} from './app.component';
import { ROUTER_PROVIDERS , 
          APP_BASE_HREF ,
          LocationStrategy,
          HashLocationStrategy
        } from 'angular2/router';


import { provide } from 'angular2/core';

import { HTTP_PROVIDERS } from 'angular2/http';

bootstrap(AppComponent, [
    HTTP_PROVIDERS,
    ROUTER_PROVIDERS,
    provide(LocationStrategy, {useClass: HashLocationStrategy})

]);

我获取数据的服务方法: 这里的基本URL指向我应该接收响应的后端休息api。

   getData(): Observable<any[]> {
    return this.http.get( this.baseUrl + this.ReceivedUrl)
                    .map(response => response.json())
                    .catch(this.handleError);

  }

并将其绑定为:

getCompany() {
        this.mainService.getData()      
                            .subscribe(
                            companys => this.companys = JSON.stringify(companys),
                            error =>  this.errorMessage = <any>error);
        }

路线配置:

@RouteConfig([

    { path: '/login', name: 'Login', component: LoginComponent},
    { path: 'base/res-company', name: 'Company', component: CompanyComponent },
])

问题: 通过在url中使用哈希位置获取html页面而不是休息服务的json数据我做错了什么

0 个答案:

没有答案