Angular JS $http method for retrieving data

No data is being displayed on the empty result page

Below is the content of the Js File:

var Movies = angular.module('Movies', []);
Movies.controller('MoviesController',['$scope', '$http', function      
($scope, $http) {
    $http.get('http://localhost:19290/CinemaAngularJs/JS/data.json')
    .success(function (data) {
      $scope.Movies = data.Movs;
    })
    .error(function (data) {
      alert("Error occurred");
    });
}]);

The Data.json File contains:

"Movs":[
{
  "name":"Mision Impossible",
  "img":"mi",
  "year":"2012",
  "short":"Mission Impossible 2012 Mission Impossible 2012 Mission Impossible  
2012 Mission Impossible 2012",
  "description":"Mission Impossible 2012 Mission Impossible"
},
{
.............
}]

The HTML File includes:

<html xmlns="http://www.w3.org/1999/xhtml" ng-app="Movies">
<head runat="server">
  <title></title>    
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/2.0.0-beta.1/angular2.min.js"></script>
  <script src="angular.min.js"></script>
  <script src="JS/controller.js"></script>
</head>
<body>
  <form id="form1" runat="server">
    <div ng-controller="MoviesController">
      <ul class="large-block-grid-4 small-block-grid-2">
        <li ng-repeat="mov in Movies">
           <h2>Name : {{mov.name}}</h2>
           <img ng-src="Img/{{mov.img}}.jpg" alt="Image Here" />
           <h3>Year : {{mov.year}}</h3>
           <h3>Short Description : {{mov.short}}</h3>
           <h3>Description : {{mov.description}}</h3>
         </li>
      </ul>
    </div>
  </form>
</body>
</html>

Identify any errors in the code and suggest steps to troubleshoot and resolve them.

Answer №1

Try running console.log(data) to verify if it is a valid JSON object or JSON string. It seems like we might need the following code snippet:

$http.get('http://localhost:19290/CinemaAngularJs/JS/data.json').success(function (data) {
    var temp = JSON.parse(data);
    $scope.Movies = temp.Movs;
    })
    .error(function (data) {
        alert("An error has occurred");
    });

Answer №2

Looks like there's an issue with your JSON format. Here's a suggested example:

{
"Movs": [{
"name": "Mision Impossible",
"img": "mi",
"year": "2012",
"short": "Mision Impossible 2012 Mision Impossible 2012Mision Impossible 2012 Mision Impossible 2012",
"description": "Mision Impossible 2012 Mision Impossible 2012Mision"
}]
}

I've checked this code and it seems okay to me.

Your Angular code appears to be correct, but you may want to consider adding validation to ensure that the result contains data and display a warning if not. Just a friendly suggestion.

Answer №3

If you encounter any issues, open the developer tools by simply pressing F12 on your keyboard while in the browser. Take note of any error messages displayed in the console window.

It's essential to check for syntax errors, although keep in mind that it may not highlight flaws in your logic!

UPDATE: There seems to be an issue with your JSON format. You can verify and correct it by pasting the code onto www.jsoneditoronline.org.

Could you modify your JSON data to look like this:

{
"Movs": [{
    "name": "Mission Impossible",
    "img": "mi",
    "year": "2012",
    "short": "Mission Impossible 2012 Mission Impossible 2012 Mission Impossible 2012 Mission Impossible 2012",
    "description": "Mission Impossible 2012 Mission Impossible 2012"
}]
}

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

Displaying blank option when presenting multiple values with ng-options in AngularJS

I've been experimenting with using ng-options to display multiple values in a drop-down menu. Interestingly, it seems to work well when displaying only one value, but encounters issues with multiple values. If you're curious, here's the co ...

Issues have been reported regarding the paramMap item consistently returning null when working with Angular 8 routing

I am encountering an issue with Angular 8 where I am trying to fetch some parameters or data from the route but consistently getting empty values. The component resides within a lazy-loaded module called 'message'. app-routing.module.ts: ... { ...

Node.js Binary Search Tree - Error: Identifier Not Found

A program run through node.js has been developed to create a binary search tree with various methods like insert, remove, and print. The program is divided into two separate files: Tree.js, which exports the functions Tree() along with its methods and test ...

How to efficiently reduce an array in ES6 without encountering any TypeScript errors

Is there a way to efficiently remove the first array element without modifying the original array (immutable)? I have this code snippet: function getArray(): number[] { return [1, 2, 3, 4, 5]; } function getAnother(): number[] { const [first, ...rest ...

The routing feature in AngularJS is functional with the hosting URL, but encounters issues when used

When attempting to access my site using the domain name, URLs with Angular routes are not functioning as expected. Interestingly, the same route works perfectly when using the hosting URL instead. For example, this URL does not work properly (redirects to ...

Is it possible for me to execute index.html using React without relying on npm start and npx create-react-app?

As I dive into learning React on my own, I find myself tangled in confusion. My initial approach was to add React to my index.html using a script like this: //index.html <!DOCTYPE html> <html lang="en"> <head> <title> ...

attempting to switch a container with a single click

I want to create a div container that expands and contracts each time an event handler is clicked. However, when the page loads, I have to click the event handler twice for it to expand the first time. After that, it works with just one click. Ideally, I&a ...

Having trouble integrating a custom dialog with DirtyForms plugin

I have successfully implemented DirtyForms, a plugin that triggers a confirmation dialog when a user tries to navigate away from a page. The base functionality of the plugin is working as intended. Now, I am trying to integrate jQuery UI's Dialog for ...

Find the total duration of all items within an array by utilizing the Moment.js library

Within an array of objects, each object contains a field named "duration" that represents a string. Here is an example of one such object: { id: 14070 project: {id: 87, name: "Test project for time tracking"} issue: {id: 10940} user: {id ...

Testing the $save() function of $resource in Angular can be done through a series of

I am currently working on a service that utilizes $resource() to make POST requests in my controller using $save(). I am encountering difficulties while attempting to write Jasmine tests for the SettingAdd() Controller. The issue lies in setting up the cor ...

Tips for associating a Json object with keys stored in a vector

I am looking to create a jsoncpp object by using keys stored in a std::vector. I need help figuring out how to do this. Please refer to the sample code below for reference. Thank you! std::vector<std::string> keys = {"a", "b", "c"}; item["a"]["b"][" ...

Is it possible to utilize the map method to extract objects that are nested multiple layers deep?

I am working on accessing the change order information in order to compile a list of all the change_order_names. The code I am currently using is yielding the results displayed below. Could someone assist me in understanding how to access the change order ...

What are the steps to parsing JSON data obtained from an API and exporting it to a CSV file?

After working with a weather API that returns data in JSON format, here is an example of the information it provides: { 'data': { 'request': [{ 'type': 'City', 'query': 'Karachi, Pak ...

How can I monitor and handle JavaScript errors without causing TestCafe tests to fail?

As I begin writing TestCafe tests, a problem arose on our website - a JS error in the console causing test failures. While it was satisfying to catch this issue, it also highlighted the fact that even minor JS errors could lead to test failures and hinder ...

Is it incorrect to append an array to a ul block using jQuery?

I am encountering an issue when trying to append an array to HTML. The array consists of HTML elements, starting with "li", then "img", and ending with "/li". When I try to append the array, it returns: <li></li> <img src="..."> Howeve ...

Transforming the time from the local timezone to Coordinated Universal Time (

I need help figuring out how to convert a Javascript date object to UTC, assuming a timezone like America/New_York: 2019-01-04T00:00:00.000Z The desired result is to convert this to a date object in UTC. 2019-01-04T05:00:00.000Z const timezone = ' ...

Runs tasks asynchronously in a sequential manner

I am attempting to run two functions asynchronously in series using node.JS, but I am struggling with the implementation. Currently, my code looks like this: Function 1: function search(client_id, callback) { clientRedis.keys('director:*', ...

Transform a list separated by commas into an unordered list

Seeking a PHP, Jquery, or JavaScript method to convert comma-separated data into an unordered list. For clarification, I have uploaded a CSV file to WordPress where one section of content is separated by commas and I am looking to display it as a list. A ...

Conceal the elements of Widget Style by using jQuery to target the ul

My task involves creating a widget style using HTML's ul tag. <div class='tabs' style='width:50%'> <ul> <li id="basic-li"><a href='#basic_information'>Basic</a></li> <li id="mas ...

What is the best way to break a card deck in Bootstrap 4 for optimal display?

Looking for a solution to display generated cards in rows with a fixed height and width. The current code I have only displays all the cards on a single row, making each card too thin: <div class="container"> <div class="card-de ...