【问题标题】:Unable to load resource无法加载资源
【发布时间】:2016-03-22 18:48:35
【问题描述】:

我只是 angularjs 的新手。我正在尝试在我的 index.html 中测试简单的控制器。但是当我在本地主机上运行它时出现错误:

加载资源失败:服务器响应状态为 404(未找到)。

这是我的文件夹结构:

angulartesting
-bower_components
-index.html
-script.js

这是我的 index.html 文件:

<!-- index.html -->
<!DOCTYPE html>
<html  ng-app="store">
<head>
  <!-- SCROLLS -->
  <!-- load bootstrap and fontawesome via CDN -->
 <link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel='stylesheet' type='text/css' />

  <!-- SPELLS -->
  <!-- load angular and angular route via CDN -->
  <script src="/bower_components/angular/angular.min.js"></script>
      <script src="/bower_components/angular-route/angular-route.js"></script>
  <script src="script.js"></script>
</head>
<body >
  <div ng-controller="StoreController as store">
    <h1>{{store.product.name}}</h1>
    <h2>${{store.product.price}}</h2>
  </div>
     <script src="bower_components/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>

这是我的 script.js:

// create the module and name it scotchApp
    // also include ngRoute for all our routing needs
(function(){
     var app = angular.module('store', []);
     app.controller('StoreController',function(){
        this.product=gem;
     });
     var gem = {
        name:'Gaurav',
        price:'220'
     }
})();    

谁能告诉我我做错了什么?
我在本地服务器上运行它。

【问题讨论】:

  • 错误来自 javascript 中的一行吗?通常伴随着 404 错误,你会得到 url 或者它找不到的文件
  • 请更新您的问题中的错误消息您得到的确切信息。
  • 不,它不是来自 @Scott Selby 的 JavaScript 行

标签: javascript angularjs


【解决方案1】:

试试这个。我刚刚删除了脚本源之前的正斜杠并添加了ng-app 属性。

    <!-- index.html -->
    <!DOCTYPE html>
    <html  ng-app="myApp">
    <head>
      <!-- SCROLLS -->
      <!-- load bootstrap and fontawesome via CDN -->
     <link href="bower_components/bootstrap/dist/css/bootstrap.min.css" rel='stylesheet' type='text/css' />

      <!-- SPELLS -->
      <!-- **Removed forward slashes before script sources** -->
      <script src="bower_components/angular/angular.min.js"></script>
      <script src="bower_components/angular-route/angular-route.js"></script>
      <script src="script.js"></script>
    </head>
    <body>
      <div ng-controller="StoreController as store">
        <h1>{{store.product.name}}</h1>
        <h2>${{store.product.price}}</h2>
      </div>
         <script src="bower_components/bootstrap/js/bootstrap.min.js"></script>
    </body>
    </html>

希望对你有帮助

【讨论】:

  • 我仍然收到同样的错误:[Tue Mar 22 2016 12:09:46 GMT+0530 (IST)] "GET /index.html" Error (404): "Not found"
  • 您尚未在本地服务器上加载 index.html。看来您没有正确配置本地服务器。您如何使用本地服务器。
  • 我已经使用 npm 包安装了 http-server,然后我进入了我的文件所在的目录,然后从终端运行 http-server。如果你能告诉我如何配置我的本地服务器,那将是一个很大的帮助。
  • 只需通过 $>npm install -g http-server 安装它,然后转到 index.html 所在的应用程序根文件夹,打开命令提示符并键入 http-server 并回车。
  • 我之前也是这样做的。
猜你喜欢
  • 1970-01-01
  • 2014-03-26
  • 2019-07-13
  • 2017-05-02
  • 2017-12-11
  • 1970-01-01
  • 2014-07-26
  • 2018-01-22
  • 2014-04-06
相关资源
最近更新 更多