【问题标题】:Invalid Json Response with valid Json [duplicate]具有有效 Json 的无效 Json 响应 [重复]
【发布时间】:2015-11-09 19:37:56
【问题描述】:

我正在尝试对数据表和 angularjs 使用 Ajax 语法

我收到一个无效的 json 响应 =

self.dtOptions = DTOptionsBuilder.fromSource([{
"id": 860,
"firstName": "Superman",
"lastName": "Yoda"
}])

但我没有收到无效的回复 =

self.dtOptions = DTOptionsBuilder.fromSource('https://l-lin.github.io/angular-datatables/data.json')
.withPaginationType('full_numbers');

这是为什么?第一项是有效的 json...在http://jsonlint.com/验证

原始示例 https://l-lin.github.io/angular-datatables/#/withAjax

谢谢!

【问题讨论】:

    标签: javascript angularjs ajax datatables


    【解决方案1】:

    问题不在于 json 的有效性,而在于 fromSource 函数接受的参数类型。 尝试创建一个本地 data.json:

    [{
       "id": 860,
       "firstName": "Superman",
       "lastName": "Yoda"
    }]
    

    然后将代码更改为

    self.dtOptions = DTOptionsBuilder.fromSource('path/to/data.json');
    

    【讨论】:

    • 上述答案可能有效。但是我认为理想的答案是 - 我需要使用 .fromFnPromise() 并返回一个承诺。我目前让它调用一个使用 $http.get 方法来生成承诺的 angularjs 服务。稍后我将研究使用 $q 来了解如何推迟承诺......
    • 是的,使用 promise api 可能会更好。查看重复问题的链接,您有一个如何使用 $q 而不是 $http 的示例,它相当简单明了。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-20
    • 2017-05-29
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多