http://localhost:4200/assets/product.json 404 (Not Found)

Discussão em 'Angular' iniciado por mohit tyagi, Outubro 27, 2024.

  1. mohit tyagi

    mohit tyagi Guest

    I am trying to read a json file data through a service.I have created the file in the assets folder named product.json. But on running the code it returns "http://localhost:4200/assets/product.json 404 (Not Found)" in the console.

    here is my service file code:

    import { Injectable } from '@angular/core';
    import { Http,Response,Headers,RequestOptions } from '@angular/http';
    import 'rxjs/Rx';

    @Injectable()
    export class ProductDataService {
    headers:Headers;
    options:RequestOptions;
    //url:string="/localservice/product.json";
    url:string = "assets/product.json"
    constructor(private _http:Http) {
    this.headers = new Headers({'Content-type':'application/json','Accept':'q=0.8;application/json;q=0.9'});
    this.options =new RequestOptions({headers:this.headers})
    }


    getAllProduct(){
    return this._http.get('../assets/product.json',this.options)
    .map((res:Response)=>res.json());
    }
    }


    I have tried using "assets/product.json","../assets/product.json" as path

    porduct.component.ts:

    export class ProductComponent {

    allProduct:product[]=[];
    constructor(private _data:productDataService) { }
    ngOnInit() {
    /* this._data.getAllProduct().subscribe(
    (data:product[])=>{
    this.allProduct=data;
    }
    ); */
    }
    onClick(value:string){
    if(value!=''){
    this.allProduct=this.allProduct.filter(res=>res.pname.startsWith(value));
    }
    else{
    this._data.getAllProduct().subscribe(
    (data:product[])=>{
    this.allProduct=data;
    }
    );
    }
    }

    }


    angular-cli.json:

    {
    "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
    "project": {
    "name": "new-app"
    },
    "apps": [
    {
    "root": "src",
    "outDir": "dist",
    "assets": [
    "assets",
    "favicon.ico"
    ],
    "index": "index.html",
    "main": "main.ts",
    "polyfills": "polyfills.ts",
    "test": "test.ts",
    "tsconfig": "tsconfig.app.json",
    "testTsconfig": "tsconfig.spec.json",
    "prefix": "app",
    "styles": [
    "styles.css"
    ],
    "scripts": [],
    "environmentSource": "environments/environment.ts",
    "environments": {
    "dev": "environments/environment.ts",
    "prod": "environments/environment.prod.ts"
    }
    }
    ],
    "e2e": {
    "protractor": {
    "config": "./protractor.conf.js"
    }
    },
    "lint": [
    {
    "project": "src/tsconfig.app.json",
    "exclude": "**/node_modules/**"
    },
    {
    "project": "src/tsconfig.spec.json",
    "exclude": "**/node_modules/**"
    },
    {
    "project": "e2e/tsconfig.e2e.json",
    "exclude": "**/node_modules/**"
    }
    ],
    "test": {
    "karma": {
    "config": "./karma.conf.js"
    }
    },
    "defaults": {
    "styleExt": "css",
    "component": {}
    }
    }


    please tell me where is the problem.Thanks in advance

    Continue reading...

Compartilhe esta Página