AngularJS TypeError:在Object.stringify(native)将循环结构转换为JSON

时间:2016-08-08 02:17:09

标签: javascript angularjs json typeerror circular-reference

我试图让这段代码正常工作。显然,代码中有一个循环引用,但我无法找到它。有人可以帮助我吗?

var appjson = '{\"APP_DATA_RETRIEVED\" : \"fail\"}';
var appPostRequest = $.get(appurl, data, appconfig);
appPostRequest.done(function(appdata) {
    appjson = JSON.stringify(appdata);
    console.log(appjson);
    var postResponse = jQuery.parseJSON(appjson);
    var postResponse2 = postResponse.Response;
    var post = [];
    console.log(postResponse2.length);
    for (i=0; i<postResponse2.length; i++) {
        var data = postResponse2[i];
        var dt = new Date(postResponse2[i]['startTime']);
        var day = (dt.getMonth() + 1) + '-' + dt.getDate() +
                   '-' + dt.getFullYear();
        if (day == date) {
            post = post.concat(data);
            console.log(data);
        }
    }
    console.log(post);
    $scope.gridOptions8.data = post;
    $scope.failchartvisible = true;
    $scope.successchartvisible = false;
    console.log($scope.gridOptions8.data);
    $scope.$apply()
});

1 个答案:

答案 0 :(得分:0)

你的JSON有问题。如果你复制并粘贴一个笔记本,你就可以看到它。

服务返回错误的对象。请在不在浏览器中的文本文档中检查您的服务结果。

enter image description here

修改

在这个例子中,我创建了小提琴,它使用 true JSON数据。

var appdata = {"Response":[{"challenge":"rp6lssenku72b2ppr4gkjb4q92","startTime":"2016-04-26 10:41:46.0","successfullyCompleted":false,"id":1,"username":"bojan1037"},{"chalennge":"ljtqvmk1mcqqqg5m0op0fljnek","startTime":"2016-04-26 10:49:56.0","successfullyCompleted":false,"id":4,"username":"bojan1037"},{"chalennge":"h062sm69lpkib7t3sk4fuppi1v","startTime":"2016-04-26 14:53:31.0","successfullyCompleted":false,"id":10,"username":"bojan1037"}],"Error":""}
var date = 1;
var appjson = JSON.stringify(appdata);
console.log(appjson);
var postResponse = jQuery.parseJSON(appjson);
var postResponse2 = postResponse.Response;
var post = [];
console.log(postResponse2.length);
for (i=0; i<postResponse2.length; i++) {
  var data = postResponse2[i];
  var dt = new Date(postResponse2[i]['startTime']);
  var day = (dt.getMonth() + 1) + '-' + dt.getDate() +
      '-' + dt.getFullYear();
  if (day == date) {
    post = post.concat(data);
    console.log(data);
  }
}
console.log(post);

https://jsfiddle.net/xnku481d/

在这个例子中,我创建了使用错误 JSON数据的小提琴。

var date = 1;
var appdata = {"Response":[{"challenge":"rp6lssenku72b2ppr4gkjb4q92","startTime":"2016-04-26 10:41:46.0","successfullyCompleted":false,"id":1,"username":"bojan1037"},{"chalenge":"ljtqvmk1mcqqqg5m0op0fljnek","startTime":"2016-04-26 10:49:56.0","successfullyCompleted":false,"id":4,"username":"bojan1037"},{"chall‌​enge":"h062sm69lpkib7t3sk4fuppi1v","startTime":"2016-04-26 14:53:31.0","successfullyCompleted":false,"id":10,"username":"bojan1037"}],"Erro‌​r":""}

    var appjson = JSON.stringify(appdata);
    console.log(appjson);
    var postResponse = jQuery.parseJSON(appjson);
    var postResponse2 = postResponse.Response;
    var post = [];
    console.log(postResponse2.length);
    for (i=0; i<postResponse2.length; i++) {
      var data = postResponse2[i];
      var dt = new Date(postResponse2[i]['startTime']);
      var day = (dt.getMonth() + 1) + '-' + dt.getDate() +
          '-' + dt.getFullYear();
      if (day == date) {
        post = post.concat(data);
        console.log(data);
      }
    }
    console.log(post);

https://jsfiddle.net/xnku481d/1/

您需要控制该数据。这是错误的。