【问题标题】:AngularJS not parsing valid JSONAngularJS 没有解析有效的 JSON
【发布时间】:2013-02-03 04:51:09
【问题描述】:

要么我错误地使用了 $resource(很可能,今天刚开始使用 angular),要么我的 JSON 不好。

AngularJS 小提琴:http://jsfiddle.net/8zVxH/1/ 和相关代码:

angular.module('app', ['ngResource']);

function AppCtrl($scope, $resource) {
var url = 'http:,,api.petfinder.com,pet.getRandom;key=260f9e3a6f6670b5d9db68b281ebe7a9;format=json;location=51503';
$scope.petfinder = $resource('http://jsonproxy.aws.af.cm/proxy/:action', 
                                 {action:url, callback:'JSON_CALLBACK'},
                                 {get: {method:'JSONP'}});

$scope.indexResult = $scope.petfinder.get();

}

jQuery Fiddle:http://jsfiddle.net/78Cab/ 及相关代码

$.ajax({
  url: 'http://jsonproxy.aws.af.cm/proxy/http:,,api.petfinder.com,pet.getRandom;key=260f9e3a6f6670b5d9db68b281ebe7a9;format=json;location=51503',
  method: 'GET'
}).done(function (data) {
  console.log(data);
});

jQuery fiddle 可以访问相同的 URL..

点击此 JSON URL http://jsonproxy.aws.af.cm/proxy/http:,,api.petfinder.com,pet.getRandom;key=260f9e3a6f6670b5d9db68b281ebe7a9;format=json;location=51503,它将 http://jsonlint.com/ 作为有效 JSON 传递

其他人有类似的问题吗?

【问题讨论】:

    标签: jquery json angularjs


    【解决方案1】:

    我想通了。即使服务返回了有效的 JSON,$resource 调用也期待 JSONP,并且不够聪明,无法使用计划 JSON。我不得不修改服务以返回回调 (JSON) 而不仅仅是 JSON。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-07
      • 2019-02-08
      • 1970-01-01
      • 2019-08-31
      • 2011-12-01
      • 2013-03-27
      • 2019-01-28
      • 1970-01-01
      相关资源
      最近更新 更多