javascript - AngularJS 从 services.js 中的文件加载 json

标签 javascript json angularjs service

我想从文件 driverStandings.json 加载 json(您可以看到它留在图像的结构中)。我使用服务 API 来加载此数据。我使用以下代码(位于图像中)。

Left the project sturcture, right the service class

当我编译时,我没有得到我的驱动程序列表,我得到以下错误列表

TypeError: url.replace is not a function
    at angular.js:8557
    at sendReq (angular.js:8426)
    at $get.serverRequest (angular.js:8146)
    at deferred.promise.then.wrappedCallback (angular.js:11682)
    at deferred.promise.then.wrappedCallback (angular.js:11682)
    at angular.js:11768
    at Scope.$get.Scope.$eval (angular.js:12811)
    at Scope.$get.Scope.$digest (angular.js:12623)
    at Scope.$get.Scope.$apply (angular.js:12915)
    at done (angular.js:8450)

什么是类型错误以及我做错了什么?

最佳答案

您不想使用 jsonp 作为静态 json 资源的方法,并且您正在嵌套另一个 $http 调用作为外部资源的 url

只需使用内部 $http 并去掉外部的

FAPI.getDrivers = function(id){
    return $http.get( ... path to json file ...)
}

关于javascript - AngularJS 从 services.js 中的文件加载 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32160673/

相关文章:

javascript - 为什么可枚举: false not cascade to inherited classes in TypeScript?

javascript - 有什么方法可以判断 float 元素是否已向下移动?

javascript - React - 从 JSON 渲染 HTML

java - JSON 到 Java HashMap 对象

css - Polymer 的纸 slider 我想更改拖动轴

javascript - AngularJs:通过单击该文本将文本设置为文本框

javascript - React 中的臃肿状态

javascript - 如何从 JavaScript 生成 Excel xlsx 文件

java - @jsonIgnore 注释不起作用

angularjs - 使用通过 angular.module().controller() 创建的 AngularJS Controller