2016-05-22 23 views
0

我正在學習如何使用Ionic,並且當前正在使用localhost來測試該應用程序。我正在嘗試構建聯繫人列表應用,並且從JSON文件加載聯繫人信息,例如姓名,電子郵件,聯繫人電話號碼和頭像。但沒有顯示出來,並在控制檯中的Chrome瀏覽器的錯誤信息這樣說:「無法加載資源:服務器與404狀態(未找到)迴應」:Ionic應用程序無法加載JSON文件

這裏是我的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文件所在的「JS」文件夾,在我的app.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": "[email protected]", 
    "personName": "Samarth Agarwal" 
    }, 

    { 
    "personContact": "+91-", 
    "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": "[email protected]" 
    } 
]} 
+0

嘗試'$ http.get('file:///android_asset/www/js/addressbook.json')' –

回答

1

我覺得你的代碼有沒有問題,只是角度已1.4.4版本後棄用原有承諾的方法success$httperror,你應該使用標準then方法,而如果你的離子取決於更高的角度版本。
然後使用ng-src而不是src當使用url插值萬一url 404錯誤。順便說一下,你的json中的personDisplayPicture屬性的url似乎很糟糕。
而對於Chrome錯誤消息,它應該是預期的cordova.js錯誤。
我建立了一個使用你的代碼的plunker project。希望這會幫助你,問候!

+0

感謝您的掠奪者演示。我嘗試添加更改,並且不再收到404錯誤。我現在只是一無所有而已。我嘗試使用代碼來製作自己的笨蛋,而且工作。所以這讓我相信我可能會錯誤地使用本地主機服務器。我在頂部有一個欄,菜單和設置圖標,但沒有顯示聯繫人數據。 – PsychoLlama