简体   繁体   English

angualr2使用http.get()请求本地json数据

[英]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) ,在阅读有关URLAngular2 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.

相关问题 Angualr2如何获取attr.data-item-code(单击) - Angualr2 how to get attr.data-item-code on (click) 将http.get数据获取到本地JSON数组变量中 - Getting http.get data into local JSON array variable 使用AngularJS $ http.get在滚动时反复请求JSON数组 - Use AngularJS $http.get to repeatedly request JSON array on scrolling 如何在angular中使用http.get方法获取json数据? - How to use the http.get method in angular to fetch the json data? 来自本地服务器上项目的servlet的Angular Http.get请求(json数据正确返回但未显示,但可与模拟api一起使用) - Angular Http.get request from servlet of project on local server (json data returns correctly, but not displaying, does work with a mock api) AngularJS将数据传递到$ http.get请求 - AngularJS passing data to $http.get request 无法使用Angular对本地JSON文件发出$ http.get请求 - Unable to make a $http.get request to a local JSON file using Angular 在 for 循环中合并 $http.get JSON 数据 - Merge $http.get JSON data in for loop 使用$ http.get()或$ http.post()通过请求传递JSON数据 - Passing JSON data with request using $http.get() or $http.post() Firebase Cloud Functions: Cannot use JSON output of http.get request via Axios - Firebase Cloud Functions: Cannot use JSON output of http.get request via Axios
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM