[英]angualr2 use the http.get() to request the local json data
Recently i am learn the angular2's http client and i have finished the demo with angular-in-memory-web-api
.Now i want to use http.get to request the local json data ,but browser tell me zone.js:1980 GET http://localhost:4200/app/hero.json 404 (Not Found)
,after read the related posts of Angular2 404 Not Found for URL: http://localhost/WebApi2/api/hero and the simliar one of angular get , i have removed the 'angular-in-memory-web-api'
, InMemoryWebApiModule.forRoot(HeroData)
and the related file from my project ,but the issue of 404 (Not Found)
is stil exists,the key code is following: 最近我正在学习angular2的http客户端,并且已经用angular-in-memory-web-api
完成了演示。现在我想使用http.get请求本地json数据,但是浏览器告诉我zone.js:1980 GET http://localhost:4200/app/hero.json 404 (Not Found)
,在阅读有关URL的Angular2 404的相关文章后:http:// localhost / WebApi2 / api / hero和与之 类似的angular get ,我从我的项目中删除了'angular-in-memory-web-api'
InMemoryWebApiModule.forRoot(HeroData)
'angular-in-memory-web-api'
, InMemoryWebApiModule.forRoot(HeroData)
和相关文件,但是仍然存在404 (Not Found)
的问题,关键代码如下:
//app.module.ts
@NgModule({
imports: [
BrowserModule,
FormsModule,
HttpModule,
JsonpModule,
],
declarations: [
AppComponent,
HeroListComponent,
HeroListPromiseComponent,
WikiComponent,
WikiSmartComponent
],
providers: [ requestOptionsProvider ],
bootstrap: [ AppComponent ]
})
export class AppModule {}
//hero.service.ts enter code here
//hero.service.ts enter code here
@Injectable()
export class HeroService {
private heroesUrl = 'app/hero.json';
constructor (private http: Http) {}
getHeroes(): Observable<Hero[]> {
// debugger;
return this.http.get(this.heroesUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
// http.get or http.post return the json obj
let body = res.json();
return body.data || { };
}
if you want all code, i have pushed it to my github.this is the address 如果您需要所有代码,我已将其推送到我的github。这是地址
anyone can help me please 任何人都可以帮助我
You need to change the webpack configuration. 您需要更改webpack配置。 Try this: 尝试这个:
{
test: /\.json$/,
exclude: /node_modules/,
use: [
{
loader: 'file-loader',
options: {
name: '[name].json'
}
}
]
}
If you are using vendor.ts file means try this: 如果您使用vendor.ts文件,请尝试以下操作:
import './app/hero.json';
In Service: 服务中:
@Injectable()
export class HeroService {
private heroesUrl = './app/hero.json';
constructor (private http: Http) {}
getHeroes(): Observable<Hero[]> {
// debugger;
return this.http.get(this.heroesUrl)
.map(this.extractData)
.catch(this.handleError);
}
private extractData(res: Response) {
// http.get or http.post return the json obj
let body = res.json();
return body.data || { };
}
thanks for everyone who try to help me solve the problem.just now,i found the answer which mean we should put the accessible resource in the assets file in order that user can get from the server.so i just try it and the problem was solved. 感谢所有尝试帮助我解决问题的人。就在此时,我找到了答案,这意味着我们应该将可访问资源放在资产文件中,以便用户可以从服务器获取。因此,我尝试一下,问题是解决了。 wish this is helpful for everyone who encounter the problem; 希望这对遇到问题的每个人都有帮助; the answer i saw 我看到的答案
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.