AngularJS TypeError: Converting circular structure to JSON at Object.stringify (native)

爷,独闯天下 提交于 2019-12-23 03:22:41

问题


I'm trying to get this piece of code working. Apparently, there is a circular reference in the code, but I can't find it. Can someone help me out?

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:


You have problem at your JSON. If you copy and paste it an notebook you can see it.

Service return a wrong object. Check your service result in a text document not in a browser.

EDİT:

In this example i create fiddle which is use true JSON data.

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/

In this example i create fiddle which is use wrong JSON data.

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/

You need to control that data. It is wrong.



来源:https://stackoverflow.com/questions/38820441/angularjs-typeerror-converting-circular-structure-to-json-at-object-stringify

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!