【问题标题】:how to read file for json file in ionic如何在离子中读取json文件的文件
【发布时间】:2016-09-16 10:32:18
【问题描述】:

我正在尝试从 url 获取 json 文件并在 ionic 中显示它,但它不工作 .我希望 json 数据在页面加载时显示,当我将数据分配给变量时 手动它可以工作,但是当我尝试从 url 执行它时,它不会加载 attall 。

contoller.js 类。

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

.controller('ChatsCtrl',function($scope,$http) {
 $scope.data = {};
  $scope.chats = $http.get('localhost/angl/file.json').success(function(res)){
   return res.data;
});

})

当我像这样直接尝试它时,它可以工作

第二个controller.js 类

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

.controller('ChatsCtrl',function($scope) {


  $scope.chats=[{
    id: 0,
    name: 'Ben Sparrow',
    lastText: 'You on your way?',
    face: 'img/ben.png'
  }, {
    id: 1,
    name: 'Max Lynx',
    lastText: 'Hey, it\'s me',
    face: 'img/max.png'
  }, {
    id: 2,
    name: 'Adam Bradleyson',
    lastText: 'I should buy a boat',
    face: 'img/adam.jpg'
  }, {
    id: 3,
    name: 'Perry Governor',
    lastText: 'Look at my mukluks!',
    face: 'img/perry.png'
  }, {
    id: 4,
    name: 'Mike Harrington',
    lastText: 'This is wicked good ice cream.',
    face: 'img/mike.png'
  }];


})

但我想从 url 读取它帮助我

这是我收到的错误消息

Failed to load resource: net::ERR_EMPTY_RESPONSE (23:37:59:154 | error, network)
  at http://localhost:8383/someApp/cordova.js
Uncaught SyntaxError: Unexpected token ) (23:37:59:504 | error, javascript)
  at www/js/controllers.js:7
Uncaught Error: [$injector:modulerr] Failed to instantiate module starter due to:
Error: [$injector:modulerr] Failed to instantiate module starter.controllers due to:
Error: [$injector:nomod] Module 'starter.controllers' is not available! You either misspelled the module name or forgot to load it. If registering a module ensure that you specify the dependencies as the second argument.
http://errors.angularjs.org/1.5.3/$injector/nomod?p0=starter.controllers

【问题讨论】:

  • 您想显示来自 res.data 的 JSON 吗?你有什么看法?
  • 如果它根本没有显示,那么您的控制台中应该有错误消息。这可能是由于 CORS。但只需检查您的控制台并在此处粘贴任何错误消息。

标签: json mobile ionic-framework


【解决方案1】:
$http.get('localhost/angl/file.json').success(function(res)){
     $scope.chats = res.data
});

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 2018-10-20
    • 2019-03-20
    • 2018-02-11
    • 2019-04-17
    • 1970-01-01
    相关资源
    最近更新 更多