Dante Jiang
Dante Jiang

Reputation: 177

Angular4 importing local js file indicates 404 not found

I set up a new project using "ng new xxx", just add one line in index.html:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Bbb</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">

</head>
<body>
  <app-root></app-root>
 <script type="text/javascript" src="./CitrixHTML5SDK.js"></script>
</body>
</html>

Then put the JS file in the same dir as index.html

the same dir

But when I run "ng server", the browser always show: 404

the js file not found

Does anyone know why? Thanks in advance.

Upvotes: 1

Views: 651

Answers (2)

Lalit
Lalit

Reputation: 1369

If you want to create a new folder i.e. "data". Then you will have to add "data" in "assets" array of angular-cli.json file. See below JSON data :-

    "apps": [
    {
      "root": "src",
      "outDir": "dist",
      "assets": [
        "assets",
        "data",
        "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": ["data/sample.js"],
      "environmentSource": "environments/environment.ts",
      "environments": {
        "dev": "environments/environment.ts",
        "prod": "environments/environment.prod.ts"
      }
    }
  ],

After that don't forget to restart the server. Otherwise you will get 404 error.

Upvotes: 0

Saurabh Agrawal
Saurabh Agrawal

Reputation: 7749

I was going through same issue with JSON file, so what I done is kept my JSON file in to assets folder. And access it through my code.

Here in Project file review you can also verify it.

Upvotes: 1

Related Questions