AngularJS encountered an error: Trying to convert a circular structure to JSON caused a TypeError in the Object.stringify function

I've been struggling to troubleshoot this code snippet. There seems to be a circular reference causing an issue, but I haven't been able to pinpoint it. Can anyone lend a hand?

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()
});

Answer №1

If you encounter issues with your JSON data, try copying and pasting it into a text editor to examine it.

Make sure to verify the output of your service in a text document rather than just relying on a web browser.

https://i.sstatic.net/LSUK4.png

UPDATE:

To illustrate this point, I have created a fiddle using correct 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/

I have also created a fiddle demonstrating incorrect 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/

It is important to validate your data to avoid any discrepancies.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

PHP code for sending email with attachment file

I have created an HTML form with the option to upload a file and send it to me via email. Additionally, I already have a PHP script that sends textbox values and text area values via email. Now, I would like the uploaded file from the form to be sent to m ...

Is the text in the React chat application too lengthy causing a bug problem?

In my chat application built with React, I am facing an issue where if a user types more than 100 characters, the message gets cut off. How can I fix this problem? Please refer to the image below for reference. https://i.sstatic.net/DLNyH.png {Object.keys ...

Include draggable functionality to a seating chart that is generated dynamically

I developed a function that generates table seats dynamically with equal spacing. The issue arises when I try to drag names from a list and drop them onto the seats as children. Unfortunately, I can't seem to achieve this functionality with the dynami ...

Error encountered while utilizing collectstatic to store static files in an S3 bucket with Django

I'm new to using django and attempting to execute collectstatic from the terminal (python manage.py collectstatic) to gather the static files in the S3 bucket, but encountering the error message below: $ python manage.py collectstatic C:\Users&b ...

Issue encountered when displaying various data options in the dropdown menus within a modal window

My goal is to display a modal when a button is clicked. The modal renders perfectly fine, but I am facing an issue with duplication of dropdowns inside the modal when the "add more" button is clicked. The main issues are: 1. Selecting the first option in ...

Transforming an array of strings into an array: a guide

An API call is returning an object with the following structure: data = { ... filter: "[1,2,3]" ... } I need to convert the string of array into an actual array of numbers, like [1,2,3]. Thank you! ...

The order in which JavaScript files are loaded is critical, especially when dealing with external

After experiencing issues with my script not working because it was loaded before jQuery by the client (a necessity), I am seeking a solution. My challenge lies in ensuring that my code waits for jQuery to load before execution, especially when dealing wi ...

Angular-Scroll is the go-to plugin for Foundation for Apps

I have been attempting to integrate angular-scroll (https://github.com/oblador/angular-scroll) with foundation for apps, but it seems to be unresponsive to scrolling. The scripts are all loaded correctly, but when I click on the navigation for scroll, noth ...

Mocking Multiple Instances of Classes in Jest

I am currently working on a project where I have a class that creates multiple instances of the same object. I am trying to mock this behavior in jest, but I keep encountering an error specifically for the second instance creation test. Error: expect(rece ...

Discovering the final element of ng-content while conducting tests using Protractor

I am searching for the always last element to click on. This is what I see when I inspect the page. https://i.sstatic.net/DMXQ8.png https://i.sstatic.net/ZkbER.png ...

Vue 3 list fails to refresh

Using an API, we are retrieving data: <script setup> import { onMounted, inject } from 'vue' let list = []; function initializeData() { axios .post("/some-link/here") .then((response) => { list ...

The `next()` function is successfully invoking all remaining middleware without skipping any

As a complete beginner to express routing logic (and node and js in general), I'm facing a problem that I can't seem to figure out. Despite my attempt to understand the context, it's still not clear to me. Let me try to explain. I am experi ...

Developing a dynamic user interface using an Angular framework and populating it with

I am currently learning Angular (version 1) and facing an issue with my layout. I need to dynamically change the navigation based on the type of user that is logged in. To achieve this, I make an API request when the page loads to fetch the user object dat ...

Is it possible to create Interactive Tabs using a Global BehaviorSubject?

Trying to adjust tab visibility based on different sections of the Ionic app, an approach involving a global boolean BehaviorSubject is being utilized. The encapsulated code has been condensed for brevity. In the provider/service globals.ts file, the foll ...

What is the method to identify the test case name as passed or failed in Puppeteer?

I am currently working on integrating the puppeteer-jest test framework with TestRail using TestRail API. To accomplish this task, I need to identify which tests have failed and which tests have passed. Despite searching through the official GitHub Reposi ...

Upon clicking close, the icons do not appear as expected

Currently, I am developing an application using Next.js. In this application, there is a functionality where clicking on the hamburger icon in mobile mode hides the icons of a Slide, and upon closing the hamburger, they should reappear. The overall struct ...

ordering json with php

I am attempting to organize a JSON format file based on date and time Here is the method I am trying: function sortFunction( $a, $b ) { return strtotime($a["date"]) - strtotime($b["date"]); } $input = file_get_contents('del.json&ap ...

Collapse a previously used item when a new item is opened within Angular

I've managed to create a tree structure for a sideBar Menu using this code, and it's working well. However, what I'm trying to achieve is that when a menu with submenus is expanded and the user clicks on another parent menu, the expanded sub ...

Distinguishing Data Input Formats for REST WCF Services (JSON vs XML)

So, I am experimenting with WCF and creating a restful service. Currently, users are sending me data through http post requests in json format and I have one method set up to receive this data. But now I want to configure another method to be able to acce ...

Separating buttons (two buttons switching on and off simultaneously) and displaying the corresponding button based on the data

My current project involves creating a registration page for specific courses. While I am able to display the information correctly, I am facing an issue with the ng-repeat function. The problem lies in all the Register buttons toggling incorrectly. Additi ...