2017-04-08 23 views
0

升級到角度/ cli 1.0.0之前我的服務獲取本地json文件(用於測試)工作可能與角CLI 1.0.0測試版23.現在,我得到錯誤升級到最終@ angular/cli 1.0.0:意外的標記<在JSON位置0在Object.parse()

GET http://localhost:4200/json/inputInventory/inputInventory.json 404 (Not Found) core.es5.js:1084 ERROR SyntaxError: Unexpected token < in JSON at position 0 at Object.parse()

當前服務的問題是:

.... ommitted

private inputmaterialInputUrl = 'json/inputInventory/inputMaterial.json'; 

constructor(private http: Http) { 
} 

getInputPO(): Observable<InputInventoryModel[]> { 
    return this.http.get(this.inputInventoryUrl) 
     .map((res: Response) => res.json()) 
     .catch((error: any) => Observable.throw(error.json().error || "Json error")); 

} .... omitted 

組件調用服務

.... omitted

export class InputInventoryComponent {

@ViewChild('largeModal') largeModal; 

id: any; 

public data: InputInventoryModel[]; 
public selectedItem = { 
    id: "0", 
    date: "", 
    PO: "", 
    supplier: "", 
    detail: "", 
    price: "", 
    cost: "", 
    note: "" 
}; 

constructor(public inventoryService: InventoryService, public router: Router) { 
    inventoryService.getInputPO() 
     .subscribe(items => { 
      this.data = items; 
      console.log(items); 
     }); 
} ....omitted 

和GUI通過* ngFor表達式呈現數據項目

json格式化並從json在線檢查工具檢查有效。

JSON文件:

[ 
{ "id": 1, 
    "date": "01/01/2017", 
    "PO": "NA12451", 
    "supplier": "Công Ty TNHH Việt Nam - TMT 256", 
    "detail": ["02 Máy In LINX 1200","02 Keyboard Mítumi","05 Hộp Mực In LINX1200","12 Cartridges 1033"], 
    "price": 250000, 
    "cost": 21500, 
    "note": "This is note 01 for order 01 date 12/12/2016" 

},
{ 「ID」:2, 「日期」: 「2017年1月2日」, 「PO」: 「NA12452」, 「供應商「價格」:340000, 「成本」:215000, 「:」CôngTy TNHHViệtNam - TMT 256「, 」detail「:[」05HộpMựcin LINX1200「,」12 Cartridges 1033「], 」注意「:」這是01號訂單的日期01日期12/12/2016「
},
{「id」:3, 「date」:「01/03/2017」, 「PO」:「NB12453」, 「supplier」:「CôngTy TNHHViệtNam - TMT 256」, 「detail」 :[「02MáyIn LINX 1200」,「02 KeyboardMítumi」,「05HộpM Inc LINX1200」,「12 Cartridge 1033」], 「price」:250000, 「cost」:200015, 「note」: 「這是記01階01日2016年12月12日」
} ]

我沒有遇到的問題,直到我升級到角/ CLI 1.0.0和2.4.8的角度

任何人都可以lp指出問題來自哪裏? 我很欣賞

+0

的問題是在服務器一邊,它不返回JSON,因此你得到'Unexpected token <'錯誤。打開你的chrome調試器並查看返回的結果。 –

+0

不,我沒有連接到任何後端,只需在應用程序的相同文件夾中使用json文件,通過angular/http服務調用,它在將angular-cli beta 23升級到最終1.0.0之前運行良好,回滾到angular-cli beta 23,那麼一切工作正常 – Leean

回答

0

我不知道這是什麼錯誤與升級角CLI做,但我用下面的代碼移動到ngOnInit()開始:

inventoryService.getInputPO() 
    .subscribe(items => { 
     this.data = items; 
     console.log(items); 
    }); 
+0

我已經轉移到ngOnInit,但它仍然支持。回滾到angular-cli beta 23,沒有問題。 – Leean

相關問題