AngularJS encountering issues with JSON parsing

My Play framework project utilizes AngularJS for its views. The controller responsible for fetching data sends 2 requests, each returning a JSON block. While the first request works fine and displays the data correctly, the second request's data gets distorted by Angular (as shown below).

The JSONs are generated accurately before rendering, as evidenced by the application log. Here is the correct JSON (extracted from the Play Framework routed method's log):

{"id":5,"name":"auditoria","url":null,"version":1,"methods":[]}

This is how AngularJS interprets it. It tokenizes :

[{},{"0":"a","1":"u","2":"d","3":"i","4":"t","5":"o","6":"r","7":"i","8":"a"},{},{},{"length":0}]

Below is the snippet of the controller:

app.controller("ViewCtrl", [ "$scope", "$resource", "$routeParams", "apiUrl",
        function($scope, $resource, $routeParams, apiUrl) {

            var ServiceList = $resource(apiUrl + "/services");
            $scope.services = ServiceList.query(); //JSON is displayed properly

            if ($routeParams.id) {

                jsonServicoQuery = apiUrl + "/services/" + $routeParams.id
                var Service = $resource(jsonServicoQuery);
                $scope.currentService = Service.query(); //JSON is butchered
            }
        } ]);

Here's the HTML structure:

<div class="row">
    <div class="col-md-3">
        <div class="bs-sidebar hidden-print" role="complementary">
            <ul class="nav bs-sidenav">
                <li><a href="/create"><i class="fa fa-plus"></i></a></li>
                <li ng-repeat="s in services| orderBy:'name'"><a
                    href="/view/{{s.id}}">{{s.nome}}</a>     
                    <ul>
                        <li ng-repeat="m in s.methods| orderBy:'name'">{{m.name}}
                            [{{m.id}}]</li>
                    </ul></li>
            </ul>
        </div>
    </div>

    <div class="col-md-9" role="main">
        <div class="bs-docs-section">
            <div class="page-header">
<!-- displaying the whole currentService JSON for debugging purposes -->
            {{currentService}} 
            </div>
        </div>
    </div>
</div>

Can anyone identify what I might be doing incorrectly?


Update: Service.query() triggers the method directed by Play Framework.

Route configuration:

GET      /api/services/:id              controllers.Services.show(id: String)

And the implementation of

controllers.Services.show(id: String)
:

public static Result show(String id) {
    Service s = Service.findById(id);

//The correct JSON is displayed here, see log below
    Logger.info("At Services show, json " + Json.toJson(s)); 

    return ok(Json.toJson(s));
}

Log:

[info] application - At Services show, json {"id":5,"name":"auditoria","url":null,"version":1,"methods":[]}

Answer №1

After careful investigation, I discovered the solution to the issue at hand. The correct code snippet should have been

$scope.currentItem = Item.fetch();
rather than
$scope.currentItem = Item.lookup();

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

Persisting dynamically generated table information into a multidimensional array

I've created a dynamic table and now I'm trying to extract the data from it and store it in a multidimensional array. However, I keep encountering an exception/error that says "Cannot set property of 0 to undefined". https://i.sstatic.net/W8B9j.p ...

What is the best way to incorporate a popover into a fullcalendar event displayed on a resource timeline in Vue while utilizing BootstrapVue?

I need help adding a popover to an event in a resource timeline using fullcalendar/vue ^5.3.1 in Vue ^2.6.11 with ^2.1.0 of bootstrap-vue. Although I found some guidance on Stack Overflow, the solution involving propsData and .$mount() doesn't feel l ...

Navigating through scroll bars in Reactjs

Currently, I am working on developing a React application, and I want to incorporate a full-screen title page as one of its key features. The challenge I am facing is related to the scrolling behavior on the title page. My goal is to have the page automati ...

Cookie vanished post registration in the captive portal

Just a heads up, I'm new to this. I've encountered an issue where a cookie doesn't persist after captive portal login. The setup involves landing on our web server, storing MAC and a unique ID on two cookies, redirecting for authentication, ...

Guide on transferring selected table row using checkbox in HTML and sending the information to views.py file in Django

Hi there, I'm new to Python-Django and in need of help. I want to be able to pass the selected table row from a template to views.py with checkboxes checked using the POST method. Additionally, I would like to be able to select multiple rows. After su ...

What is the best way to merge an array into a single object?

I have an array object structured like this. [ { "name": "name1", "type": "type1", "car": "car1", "speed": 1 }, { "name": &q ...

Issue with nextjs not returning the updated object correctly

I'm currently developing a nextjs app that incorporates authentication. There are two key functions that I execute on each page load. The first function checks for the existence of jwt cookies and then calls another function to validate the tokens if ...

Retrieve an object that includes a property with an array of objects by filtering it with an array of strings

I have a large JSON file filled with data on over 300 different types of drinks, including their ingredients, names, and instructions. Each object in the file represents a unique drink recipe. Here is an example of how one of these objects is structured: ...

A guide on iterating through added inputs within an array

I am working on submitting multiple appended inputs through an API. The issue I'm facing is that the number of items varies each time. Upon form submission, only the last appended input is successfully submitted, and I receive an object in return. How ...

What value does a variable have by default when assigned to the ko.observable() function?

I am in the process of learning KnockoutJS and I have implemented code for folder navigation in a webmail client. In the view code, there is a comparison being made to check if the reference variable $data and $root.chosenFolderId() point to the same memor ...

Implementing a switch to trigger a JavaScript function that relies on a JSON object retrieved from a GET request

Having some trouble using a toggle to convert my incoming Kelvin temperature to Celsius and then to Fahrenheit. It loads properly as default Celsius when the page first loads, but once I try toggling the function outside of locationLook, it doesn't se ...

How to globally register components in Vuejs located in subdirectories

I've been working through the documentation on the Vuejs website to understand how to globally register vue components. In my setup, I've specified that the components folder's relative path is ./global and I've set it to look into sub ...

Encountering a 404 error when utilizing json-server

Exploring the json-server npm package has been an interesting journey for me. I am currently working with the latest version of json-server, which is 0.17.0. My goal is to retrieve data from the URL "http://localhost:3001/getFolderByFolder/14", where t ...

Having trouble retrieving JSON data from CakePHP 3 controller?

I am encountering an issue while trying to send a query from a controller to the view file in order to utilize that data within my form. Despite the fact that the function is functioning correctly and returning the accurate data, it is throwing an error. ...

I am having trouble with my append function even though I checked the console log and did not see any errors (beginner's inquiry)

As I practice working with functions and dynamically creating input fields, I have encountered an issue where I am unable to append my input field to the form. Despite using console.log() and not seeing any errors, I can't figure out what mistake I ma ...

Display Quantity of Current Website Visitors

Similar Question: how to track website visitors using java script or php I am looking to retrieve the current number of viewers on a webpage that has an embedded stream. Is there a method to accomplish this utilizing PHP and AJAX, in order to display ...

Tips for getting a plugin to function properly when the page is refreshed in Nuxt app

I am currently incorporating the vue GAPI plugin into my project. While it functions smoothly when navigating between pages, I encounter an error upon refreshing: vue-gapi.common.js?15fd:241 Uncaught (in promise) Error: gapi not initialized at GoogleAuth ...

updating the v-model in Vue.js datepicker retains the previously set value while setting a new date

When using the template, the endDate updates as expected. However, there seems to be an issue when the filtersChanged method is called with the @selected attribute - the updated value is not the new one but rather the previously set value. <template&g ...

Webpack-hot-middleware increases the number of event listeners exponentially

When configuring my new development environment (node server + client with vanilla js), I encountered an issue with webpack-hot-middleware for live reloading front-end changes. The problem arose when using code like: $button.addEventListener('click&a ...

Experiencing difficulties with the app.post function is leading to the error message saying "Cannot

Currently, I'm facing an issue with the functionality of "app.post" while working on building a sign-up page that allows users to input their information. This is the code snippet I've put together so far: const express = require("express"); con ...