【问题标题】:Ionic app cannot load JSON file离子应用程序无法加载 JSON 文件
【发布时间】:2016-05-22 06:47:14
【问题描述】:

我正在学习如何使用 Ionic,目前正在使用 localhost 来测试应用程序。我正在尝试构建一个联系人列表应用程序,并且从 JSON 文件加载联系人信息,例如姓名、电子邮件、联系电话号码和头像。但什么也没显示,控制台中的 Chrome 错误消息显示:“加载资源失败:服务器响应状态为 404(未找到)”

这是我的 index.html 文件:

   <!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="starter">

    <ion-pane ng-controller-"myController">
      <ion-header-bar class="bar-dark">
      <button class="button button-clear icon ion-navicon"></button>
        <h1 class="title">Friends List</h1>
        <button class="button button-clear icon ion-settings"></button>
      </ion-header-bar>
        <div class="bar bar-subheader item-input-inset">
            <label class="item-input-wrapper">
              <i class="icon ion-search placeholder-icon"></i>
              <input type="text" placeholder="Search"/>
            </label>
        </div>
      <ion-content class="has-subheader">
        <div class="list">
          <div class="item item-thumbnail-left" ng-repeat="element in data.persons">
            <img src="{{ element.personDisplayPicture }}"/>
            <h2>{{ element.personName }}</h2>
            <p>{{ element.personContact }}</p>
            <p>{{ element.personEmail }}</p>
          </div>

        </div>
      </ion-content>
    </ion-pane>
  </body>
</html>

这是我的 app.js 文件:

angular.module('starter', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {

      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);


      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})

.controller('myController', function($scope,$http){

  $http.get('js/addressbook.json').success(function(data){
    $scope.data = data;
  })

})

我的 addressbook.json 文件位于我的 app.js 文件所在的“js”文件夹中。 addressbook.json 文件只使用了一些示例信息,如下所示:

{"persons": [
  {
    "personContact": "+91-9997725251",
    "personDisplayPicture": "https://fbcdn-sphotos-b-a.akamaihd.net/hphotos-ak-xtf1/v/t1.0-9/1509974_10205886752870556_6275779368926452443_n.jpg?oh=d407cc6c85c581f4ee96cd65ea15a171\u0026oe=55ECD16C\u0026__gda__=1443235524_024ba60447125011575b739fb45d23c4",
    "personEmail": "samarthagarwal@live.com",
    "personName": "Samarth Agarwal"
  },

  {
    "personContact": "+91-9874563210",
    "personDisplayPicture": "https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xfp1/v/t1.0-1/p160x160/10997379_828487523879435_5609581450404507062_n.jpg?oh=109f958adce1bb72754ed1772e598893&oe=56276727&__gda__=1441608268_136ec1af8e25e4364909decbd724f26a",
    "personName": "Apoorva Agarwal",
    "personEmail": "apoorva@gmail.com"
  }
]}

【问题讨论】:

  • 试试$http.get('file:///android_asset/www/js/addressbook.json')

标签: jquery angularjs json ionic-framework


【解决方案1】:

我认为您的代码没有问题,除了 Angular 在 1.4.4 版本之后已弃用 $http 的旧承诺方法 successerror 之外,如果您的 ionic 依赖于更高的角度版本。
然后在使用 url 插值时使用 ng-src 而不是 src 以防 url 404 错误。顺便说一句,您的 json 中 personDisplayPicture 属性的 url 似乎效果不佳。
对于 Chrome 错误消息,它应该是预期的 cordova.js 错误。
我已经建立了一个使用您的代码的plunker project。希望这对你有帮助,问候!

【讨论】:

  • 感谢 plunker 演示。我尝试添加您的更改,但我不再收到 404 错误。我现在只是什么都看不见。我尝试使用代码制作自己的 plunker,并且成功了。所以这让我相信我可能错误地使用了 localhost 服务器。我在顶部有栏,还有菜单和设置图标,但没有显示联系人数据。
猜你喜欢
  • 2019-10-03
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 2020-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-09-27
  • 2018-02-02
相关资源
最近更新 更多