The Proper Method of Displaying Data from a JSON File Using AngularJS

I'm having trouble getting the data from a JSON file (click on the "Json File" link to view the structure). I'm not sure what to put after "$Scope.listOfRecipe=". I tried using response.data.recipes, but it's not working and causing errors.

angular.js:12520 TypeError: Cannot read property 'recipes' of undefined
      at recipesController.js:10
      at angular.js:10296
      at angular.js:14792
      at r.$eval (angular.js:16052)
      at r.$digest (angular.js:15870)
      at r.$apply (angular.js:16160)
      at g (angular.js:10589)
      at T (angular.js:10787)
      at XMLHttpRequest.w.onload (angular.js:10728)

Here is the Json File for reference.

This is my recipesControllers.js

var myApp = angular.module('myApp', []);

 myApp.controller('namesCtrl',function ($scope, $http) {

$scope.listOfRecipe = null;

$http.get('http://164.132.196.117/chop_dev/recipe/recipes.json')
     .success(function (response) {
         $scope.listOfRecipe = response.data.recipes;
     })

});

This is my Index.html

<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js">
</script>

<body>
  <div ng-app="myApp" ng-controller="namesCtrl">
    <ul>
      <li ng-repeat="x in listOfRecipe ">
        {{ x.Recipe.id + ', ' + x.Recipe.name }}
      </li>
    </ul>
  </div>
  <script src="C:/Users/Enetfirm  Server/Desktop/AngularJs/recipesController.js"></script>
</body>

</html>

Answer №1

To determine the response, you can utilize console.log(response) and make necessary adjustments to your code.

However, it is advisable to follow the standard promise pattern:

$http.get('http://164.132.196.117/chop_dev/recipe/recipes.json')
  .then(function(resp) {
    $scope.listOfRecipe = resp.data.recipes;
  })
  .catch(function(errResp) {
    // handle errors
  })
  .finally(function() {
    // upon completion, perform actions in the view
    // such as removing a spinner gif or scrolling to top
  });

Answer №2

Have you confirmed that the file is downloading in the developer tools network tab?

Ensure it is functioning correctly, and be sure to console.log both response and response.data to confirm they are present!

Since it's a JSON file, the response itself is the JSON and not res.data.

Please review and respond with your solution.

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

When I click on my div, the color does not change as expected

Recently, I wrote a code snippet where there are 9 different div elements each assigned a ".spaces" class. The intention was to change the color of these divs upon clicking on them. However, I encountered an issue where only the first div changes its color ...

Exploring the use of asynchronous data retrieval with jQuery and JSON within MVC 2.0

Attempting to retrieve server-side data using jQuery's getJSON method has hit a snag. The URL specified in the getJSON call is being reached, but the expected result is not being returned to the browser upon postback. There are suspicions that the iss ...

a guide on expanding a submenu in a shiny dashboard sidebar without using automated functions

I am facing a challenge in manually expanding a submenu within a sidebar on shiny dashboard. The function updateTabItems does not seem to work with nested menus, only with normal menus. For example, when I click on 'Switch tab', it switches the ...

Utilize toggle functionality for page rotation with rxjs in Angular framework

Managing a project that involves a container holding multiple cards across different pages can be overwhelming. To address this, the screen automatically rotates to the next page after a set time interval or when the user presses the space bar. To enhance ...

Adjust the pagination length within the jQuery DataTables plug-in

I am looking to customize the pagination length in DataTables plug-in for jQuery. Specifically, I want to calculate the number of pages on the server side and display the correct amount of buttons on the client side. Can anyone provide guidance on how to ...

Modify the divs in two separate occasions when submitting different forms

Within my form, I have radio buttons located inside div1, a captcha code to be solved in div2, and additional content in div3. My goal is to have div1 replaced by div2 when a radio button is checked and submitted. Then, after completing the captcha in div2 ...

Is there a way to prevent redundancy when exporting functions in Node.js?

Is there a way to avoid repeating module.exports or usuariosControllers when writing a small API in express for fun? module.exports.getUsuarios = (request, response) => {}; module.exports.crearUsuario = (request, response) => {}; module.exports. ...

Redirecting in Next.js from an API route

I am in the process of developing a backend application that necessitates user authentication. Within this project, I'm utilizing 2 external APIs: API A: responsible for managing user accounts and sessions API B: utilized for executing CRUD operation ...

Avoid changing the regex pattern if it is surrounded by a specific character

I want to change all occurrences of strings enclosed by - with strings enclosed by ~, unless the string is again surrounded by *. For instance, consider this string... The -quick- *brown -f-ox* jumps. ...should be altered to... The ~quick~ *brown -f-ox ...

Collect data from a third-party website that necessitates the activation of JavaScript

Given that phantomjs has been abandoned, I am exploring alternative methods. For instance, using chrome-webdriver may not be ideal as it cannot operate on a remote host like heroku. Is there a way to scrape a website that relies on JavaScript being activa ...

The JavaScript code that added links to the mobile menu on smaller screens is no longer functioning properly

I recently created a website with a mobile navigation menu that should appear when the browser width is less than 1024px. However, I used some JavaScript (with jQuery) to include links to close the menu, but now the site is not displaying these links and t ...

Capturing all requests - javascript

I have a webpage called sample-page.html that contains 2 links: sample-page.html - link1 (GET, AJAX) - link2 (GET, new page) Clicking on link1 triggers an AJAX GET request and remains on the same page (sample-page.html). Clicking on l ...

I am having trouble getting the filter functionality to work in my specific situation with AngularJS

I inserted this code snippet within my <li> tag (line 5) but it displayed as blank. | filter: {tabs.tabId: currentTab} To view a demo of my app, visit http://jsfiddle.net/8Ub6n/8/ This is the HTML code: <ul ng-repeat="friend in user"> ...

Error message "Uncaught in promise" is being triggered by the calendar function within the Ionic

Can someone assist me in creating a calendar feature for my app? My concept involves a button with text that, when clicked by the user, opens a calendar. However, I am encountering an error message: ERROR Error: Uncaught (in promise): TypeError: Cannot set ...

Obtaining JSON data created through the use of the stringify method

After retrieving data from a database, I stored it in an array. To convert this array into JSON format, I used the stringify method which resulted in the following JSON structure: var view_data = [{ f o o = " boo " , t e x t = "t e s t " }]; However, w ...

managing access and navigation in an angular program using a boolean variable

In order to restrict and redirect access to specific routes in my angular application, I am looking for the simplest solution that works with the current versions of angular and firebase. Previous solutions have become outdated due to the continuous evolut ...

Unable to retrieve any data while consuming a RESTful web service with AngularJS

As a novice in Angularjs, I am seeking assistance with examples for the following: The added script is as follows: var app = angular.module('myapp', []); app.controller('MyCtrl1', ['$scope', 'UserFactory', functio ...

"Strategies for using Selenium in Python to simulate clicks without relying on class, id, or name attributes

I am currently attempting to locate the "X" button and click on it, but I am struggling to find a way to do so. Below is the HTML code for the element: <div style="cursor: pointer; float:right; border-radius: 3px; background-color: red; font-size: ...

Tips for extracting JSON data from a JSON array into a RecyclerView in Java on Android devices

I am facing an issue with parsing JSON data into my RecyclerView. Whenever I encounter JSON data that contains array data, my app crashes when attempting to retrieve it. Does anyone have a solution for this problem or perhaps a sample code with a similar s ...

React ES6 SystemJS encountered an unforeseen token error that couldn't be caught

Even though I have imported react and react-dom using the System.config setup below, I am still encountering the error mentioned here: Uncaught (in promise) Error: Unexpected token <(…) Here is the HTML structure: <!DOCTYPE html> <html l ...