使用angular资源读取json文件时出错

error when reading json file using angular resource

本文关键字:文件 出错 json 读取 angular 资源 使用      更新时间:2023-09-26

我试图使用angular ngResource模块从json文件中获取数据,但我在控制台上得到404错误- URL正在与调用get函数后的代码连接。

错误如下:

localhost前缀/test_data/data.json/函数% 20(数据)7 b % % 20% 20% 20% 20% 20% 20% 20% 20% 20% 20% 20% 20% 20 console.log(数据);% 20% 20% 20% 20% 20% 20% 20% 20% 7 d

下面是我使用的代码:

json服务-

angular.module('jsonServices',['ngResource']).factory('JsonService', 
    function ($resource) { 
    return $resource('/test_data/data.json'); 
});

使用服务的控制器:

angular.module('myApp.controllers')
.controller('mainController', function ($scope, JsonService) {
        JsonService.get(function (data) {
            console.log(data);
        });
});

启动这一切的app js:

angular.module('myApp',['myApp.controllers','jsonServices','ui.router'])
.config(function($stateProvider, $urlRouterProvider){
    $stateProvider.state('home',{
        url:'/home',
        template:'some template path goes here',
        controller:'mainController'
    };
});

你可以看到"function(data)"作为字符串被添加到url中。还有一件事是,当我尝试访问json文件浏览到它的位置,我可以看到json文件的内容OK。

有人遇到过这个问题并找到解决方法吗?是我做错了什么还是遗漏了什么?

谢谢,

伊兰

发现问题,似乎我使用了一个更低的包调用ng-resource而不是angular-resource。