【问题标题】:json request with angularjs带有angularjs的json请求
【发布时间】:2014-03-01 23:23:51
【问题描述】:

我正在寻找一个带有 json 文件的 AJAX 请求,但它没有被解析。

我的控制器:

function troupesController($scope, $routeParams, $http) {
 $http.get('js/data/troupes.json').success(function(data) {

     console.log(data);
     console.log(data.test);
     $scope.troupes = data;
});

}

App.js:

var app = angular.module('app', ['ngRoute', 'ngAnimate', 'angular-carousel']);
app.config(function($routeProvider){
$routeProvider
    .when('/home', {templateUrl: 'views/home.html'})
    .when('/about', {templateUrl: 'views/about.html'})
    .when('/batiments_troupes', {templateUrl: 'views/batiments_troupes.html'})
    .when('/troupes', {templateUrl: 'views/troupes.html', controller: 'troupesController'})
    .when('/unitee', {templateUrl: 'views/unitee.html'})
    .otherwise({ redirectTo:'/home'});

});

json 文件:

{
'test':'toto;'
'troupes' : [
    {
        'name': 'Barbare', 
        'basic_image': 'troupes/barbare/base_Barbarian.png'
    }
]

};

console.lg 的结果是:

 {
'test':'toto;'
'troupes' : [
    {
        'name': 'Barbare', 
        'basic_image': 'troupes/barbare/base_Barbarian.png'
    }
]

}; 未定义

【问题讨论】:

  • JSLint 说你的 JSON 文件无效,从那里开始(双引号!)

标签: ajax json angularjs


【解决方案1】:

在 JSON 中进行了一些更正

 {
    "test":"toto",
    "troupes" : [
         {"name" : "Barbare",
          "basic_image" : "troupes/barbare/base_Barbarian.png"
         }
    ]
 }

【讨论】:

    猜你喜欢
    • 2023-03-26
    • 2020-12-13
    • 2016-05-06
    • 1970-01-01
    • 1970-01-01
    • 2016-07-09
    • 1970-01-01
    • 2018-12-11
    • 1970-01-01
    相关资源
    最近更新 更多