AngularJS controller facilitates HTTP requests

I have been attempting to display data fetched from a server. I created a controller and tried assigning the response to a scope property, using both 'response' and 'response.data', but neither method worked.

Here is how the HTML file appears:

<!DOCTYPE html>
<html>
<head>
    <script src="angular.min.js"></script>
    <title>Service Call ...</title>
</head>

<body>
    <script>
        var app = angular.module('myApp', []);

        app.controller('customersCtrl', function($scope, $http) {
            $http.get("https://linkToServer.json")
                .success(function(response) {$scope.trainings = response.data;
            });
        });
    </script>

    <div ng-app="myApp" ng-controller="customersCtrl">  
        <ul>
            <li ng-repeat="tr in trainings">
                {{ tr.id }}
            </li>
        </ul>
    </div>
</body>
</html>

The JSON structure resembles this:

[
  {
    "id" : "sm1001",
    "name" : "Lu",
  },
{
    "id" : "lm9898",
    "name" : "Di",
  },
  ....
]

What could be the issue?

Plunkr showcases the problem

Answer №1

Your solution worked perfectly after adjusting the JSON format. The JSON code was not included in the initial sample, so I had to copy it from your question.

"name" : "Lu",

needs to be

"name" : "Lu"

http://plnkr.co/edit/toSicojh58y1lWrziyI8?p=preview

This may have been just a mistake in the sample data provided. The angular code is functioning correctly.

Answer №2

It seems like the issue lies in the structure of your HTML code. Starting with a div, followed by a head tag I have made some adjustments to your plunkr, and it appears to be functioning properly now: http://plnkr.co/edit/4P7hT5oINMkS82rgtGa1?p=preview

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

The database has unfortunately registered an incorrect value for the "date" input after it was saved

When I select a date from the datepicker input field, it is being incorrectly saved in the database. I am selecting the date using the datepicker and then using AngularJS to send it to Spring MVC AngularJS: $scope.updateProjectDetails = function(detail) ...

Can someone clarify the meaning of (e) in Javascript/jQuery code?

Recently, I've been delving into the world of JavaScript and jQuery to master the art of creating functions. I've noticed that many functions include an (e) in brackets. Allow me to demonstrate with an example: $(this).click(function(e) { // ...

Updating the state value of a variable that utilizes a custom Hook: a step-by-step guide

After watching a video by Dan Abramov, I decided to optimize my component by creating a custom Hook called useFormInput. This hook handles the state and onChange functionality for all of my form input fields. Everything was working perfectly until I neede ...

Tips on utilizing external libraries like webcam.js in AngularJS version 1.5.2

As a newcomer to AngularJS, I am attempting to connect to the webcam using AngularJS. To achieve this, I have incorporated the third-party library webCam.js and utilized the directive found at https://github.com/bcabanes/ng-camera. However, I encountered ...

Sort the collection of words by their corresponding suffixes

Looking to use JavaScript to compare two lists and extract words from WORDLIST that end with the characters in END (not in the middle of the word). I am also open to using jQuery for this task. var WORDLIST = ['instagrampost', 'facebookpost& ...

Utilize multiple onChange events in React - one for validating user input and another for formatting the input

I'm looking for a solution to format a Phone number field as (123) 456-7890. Currently, I am using a common handleChange function to handle all input onChange events and JOI for validation. Can someone please assist me with this? import React from &qu ...

Achieving success despite facing rejection

I am currently utilizing the bluebird settle method in order to verify results for promises without concerning myself with any rejections. Interestingly, even though I have rejected the promise within the secondMethod, the isFulfilled() still returns tru ...

How can I show the unique phrase "Today" on a specific date (date=today) with the Angular-UI-Bootstrap Datepicker?

I'm currently integrating the Angular-UI datepicker directive into my project and I'm facing an issue. I want to have the datepicker display 'Today' instead of the actual date like 2015/04/27. Is there a way to achieve this without hav ...

JavaScript Canvas: Using the lineTo() method in drawing lines

I am embarking on a journey to learn Javascript! Can someone guide me on how to link a variable to the current xy coordinates in order to utilize relative positions for drawing lines? My goal is to create an etch-a-sketch using keyboard inputs - specifica ...

Ways to eliminate submenu tooltips

Whenever I hover over a menu item with submenu pages in the wordpress backend, a "tooltip" pops up showing each submenu page. How can I remove these tooltips? I have attempted to remove the wp-has-submenu style class, which somewhat works. The tooltip no ...

Summon the keyboard to appear

How do I make the keyboard appear on my website? I have a javascript function that recognizes keyboard input, but I am struggling to display the keyboard on my mobile device. Is there a way to simulate traditional input and generate key events? I should ...

Navigating and retrieving information from JSON data

I'm working with JSON data that I have received: [{"pk": 7, "model": "pycourt_login.orders", "fields": {"status": 0, "delivered": false, "order_id": "6count1%2", "student_id": 5, "counterid": "counter1", "datetime": "2011-04-05 01:44:01", "dish": 6, ...

I want to create a custom jQuery slider totally from scratch

Greetings everyone, I have been tasked with creating a slider using only HTML / jQuery code. Here is the template: And here is the HTML code for the template above: <div id="viewport-container"> <section id="sliding-container"> & ...

Accessing an API hosted on azurewebsites.net in order to retrieve a JSON file

I am looking to automate the process of downloading a JSON file from an API hosted on azurewebsites using Python. While I can manually access the website and download the file, I want to streamline this task through code. I have attempted to establish a co ...

What is the best method for retrieving a nested array within an API JSON response in a React application?

Currently struggling to access the values within a nested array returned from my API call. The API response goes through filters before being passed to the table as "dataPagedSortedAndFiltered". Upon running 'console.log("Parameters: ", dataPagedSort ...

Attempting to connect with an Elementor Form submission

I am currently working on a functionality to toggle the visibility of a download button once a user has submitted their email through a standard Elementor form. Despite Elementor offering various actions, I have found that I can only achieve this toggle us ...

Is recursion effective in this scenario? (javascript/node.js)

Currently, I am working on creating a TV using a Raspberry Pi and JavaScript to play the same playlist repeatedly. Although playing the files is not an issue, I am facing a challenge with the asynchronous nature of JavaScript. Below is the code that is cau ...

What is the best approach to changing the color of an item in a cart once it has been selected?

Recently, I've been facing an issue with changing the color of an item after it has been added to the cart. Below are my functions for adding and removing items from the cart: function addToCart(newItem) { cartItems.map(item => newItem.ty ...

"Discovering the method to showcase content based on page number or when the 'previous' or 'next'

Here is how my Vue component looks like: <template> ... <b-col v-for="item in items" v-bind:key="item.id" cols="2"> ... <strong slot="header" class="text-dark" :title ...

Uploading a three.js canvas to the server without saving it as a file

Currently, I am in the process of saving an image to the server using a three.js script that I have created. The script looks like the following: actualCode(THREE); function actualCode(THREE) { //Rendering variables const renderer = new THREE.WebG ...