我是Angular 2.0的新手,我嘗試使用@angular\cli
來構建示例應用程序。錯誤:0:0造成的:狀態響應:0爲URL:null Angular 2.0問題
因此,我使用ng serve --open
來爲localhost:4200
中的應用程序提供服務。
現在,我有一個名爲example.service.ts
服務,代碼如下:
import { Injectable } from '@angular/core';
import { Http,Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable() //Decorator
export class ExampleService {
private _url : string = "file:///Users/chiranjib/Documents/Angular%202%20Tutorial/ninja-app/apidata/employeedata.json";
constructor(private _http : Http){}
getEmployees(){
return this._http.get(this._url)
.map((response : Response) => response.json());
}
}
文件employeedata.json
駐留在apidata
文件夾內的地方。
在我component
我做的:
this._exampleService.getEmployees().subscribe((resEmployeeData) => this.employees = resEmployeeData);
但它顯示了錯誤:
即使在火狐錯誤仍然存在。
如果這樣做,
private _url : string = "apidata/employeedata.json";
錯誤顯示:
如何解決錯誤,讓我的應用程序的工作?
第一個圖像是一個cors問題,通常導致應用程序在兩個不同的域中,第二個是未找到的,這意味着無法找到資源。 – TGarrett
請包括錯誤作爲文字,而不是圖像。 –
你在應用程序文件夾中有json文件嗎?什麼是應用程序結構 – Aravind