AngularJS is not able to load JSON files

I am facing the same issue as the individual in this discussion: $http.get doesn't load JSON

Unable to find a solution either way. I am running the website using MAMP, so I don't think that's the problem?

Below is the code:

index.html

<!DOCTYPE HTML>
<html ng-app="myQuiz">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <title>Test Your Knowledge: Saturn</title>
        <link rel="stylesheet" type="text/css" href="css/quiz.css">
        <script type="text/javascript" ng-src="js/angular.min.js"></script>
        <script type="text/javascript" ng-src="js/quiz.js"></script>
    </head>
    <body>
        <div id="myQuiz" ng-controller="QuizController">
            <h1>Test Your Knowledge: <span> Saturn</span></h1>
            <div class="progress">
                {{totalQuestions}}
            </div>
        </div>
    </body>
</html>

quiz.js

(function(){
    var app = angular.module('myQuiz', []);

    app.controller('QuizController', ['$scope','$http','$sce',function($scope,$http, $sce){

        $scope.score = 0;
        $scope.activeQuestion = -1;
        $scope.activeQuestionAnswered = 0;
        $scope.percentage = 0;

        $http.get('quiz_data.json').then(function(quizData){
            $scope.myQuestions = quizData.data;
            $scope.totalQuestions = $scope.myQuestions.length;
        });
    }]);
})();

quiz_data.json

[
    {
        "question" : "Saturn is the _______ planet from the Sun.",
        "answers" : [
            { "id" : 0, "text" : "Fourth" },
            { "id" : 1, "text" : "Sixth" },
            { "id" : 2, "text" : "Second" },
            { "id" : 3, "text" : "Eighth" }
        ],
        "correct" : 1
    },
        {
        "question" : "Which image shows a close-up of Saturn?",
        "answers"  : [
            {"id"  : 0, "image" : "images/close_up_01.jpg" },
            {"id"  : 1, "image" : "images/close_up_02.jpg" },
            {"id"  : 2, "image" : "images/close_up_03.jpg" },
            {"id"  : 3, "image" : "images/close_up_04.jpg" }
        ],
        "correct"  : 3
    },
    {
        "question" : "One year on Saturn is equivalent to how many years on Earth?",
        "answers"  : [
            {"id"  : 0, "text" : "12"},
            {"id"  : 1, "text" : "6"},
            {"id"  : 2, "text" : "29"},
            {"id"  : 3, "text" : "2"}
        ],
        "correct"  : 2
    },
    {
        "question" : "What is the name of Saturn's largest moon?",
        "answers"  : [
            {"id"  : 0, "text" : "Hercules"},
            {"id"  : 1, "text" : "Europa"},
            {"id"  : 2, "text" : "Goliath"},
            {"id"  : 3, "text" : "Zeus"},
            {"id"  : 4, "text" : "Titan"},
            {"id"  : 5, "text" : "Triton"}
        ],
        "correct"  : 4,
        "feedback" : "Though the names seem similar, Triton orbits the planet Neptune."
    },
    {
        "question" : "Saturn is visible from Earth without a telescope",
        "answers"  : [
            {"id"  : 0, "text" : "True"},
            {"id"  : 1, "text" : "False"}
        ],
        "correct"  : 0
    }
]

website It seems like there is an issue with the connection to json. Unsure of how to resolve this.

Thank you in advance!

Answer №1

To access your file, you will need to utilize the local http server.

Based on the webpage image, it appears that you are currently loading your index.html from the file system rather than through your web server.

Make sure your address bar reads

http://localhost:8888/<path to your index.html>
or the specific port where your web server is active.

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

What is the process of uploading a file to Dropbox using dropbox.js?

PROBLEM WITH UPLOADING A PICTURE TO DROPBOX I am encountering difficulties uploading an image file to Dropbox from Node.js using the official dropbox.js library. I am attempting to upload an image that is stored on another server, for example, the Dropbox ...

"Facing an issue with my handleChange function not getting triggered in MaterialUI. Can anyone

Why isn't my handleChange function being invoked with the TextField component from Material UI? Even though I can input values in the text fields, the component's state is not updating. Here is the Auth component: const Auth = () => { cons ...

Sending JSON data with Python and fetching the response

I've been working on a Python script to make a post request, but I'm not receiving any response. Everything seems correct in my code, so I suspect there might be an issue with the service itself causing the lack of response. Can anyone spot if th ...

There appears to be an issue with the express Router when trying to access the '/page' route

Currently, I am in the process of creating a basic express routing system while utilizing vanilla HTML for the front-end. index.js const express = require('express'), bodyParser = require('body-parser'), serverle ...

Encountering an issue in AngularJS where a necessary dependency cannot be resolved while attempting to utilize a global service

In order to streamline my code, I decided to create a service for a function that is utilized in multiple controllers. To achieve this, I added a commonService.js file to my index.html 'use strict'; var myApp = angular.module('myAppName&ap ...

Developing a standard jQuery function for adding event listeners

Attempting to replicate the functionality of Google Maps' addListener using jQuery listeners for clicks, keypresses, etc. In Moogle Maps, you can use .event.addListener(map, 'click', function()... or switch 'click' with 'drag ...

Receive a notification for failed login attempts using Spring Boot and JavaScript

Seeking assistance with determining the success of a login using a SpringBoot Controller. Encountering an issue where unsuccessful logins result in a page displaying HTML -> false, indicating that JavaScript may not be running properly (e.g., failure: f ...

displaying a loading component in a Vue application only when necessary

Below is the vue code snippet I'm working with: <div v-if="team_members && team_members.length > 0"> screen1 </div> <div v-else> <svg viewBox="0 0 50 50" class="spinner"> ...

Enzyme fails to locate text within a div even though it is present

In my current test scenario, I have a set of data displayed in Material-UI Cards. The data is provided through a JSON array consisting of objects with fields like projectName, crc, dateCreated, createdBy, and lastModified. { projectName: 'P ...

The Admob platform is displaying test ads, but my actual ads are not appearing

I'm currently using Admob to display Android ads in my app. Strangely, my actual ads are not appearing, but when I input the test code, it works perfectly fine. Interestingly, when I tested my ads in a different game, they displayed as intended. It&ap ...

What is the best way to display two arrays next to each other in an Angular template?

bolded text I am struggling to display two arrays side by side in an angular template. I attempted to use ngFor inside a div and span but the result was not as expected. A=[1,2,3,4] B=[A,B,C,D] Current Outcome using ngFor with div and span : Using Div : ...

This element is not suitable for use as a JSX component since its return type 'void' is not a valid JSX element. Please check the return type to ensure it is compatible with

I have been working on this code snippet: function searchData(searchWord: any) { if (originalData.length > 0) { if (searchWord !== "") { setDataList([...originalData.filter((svc: any) => ...

Guide to utilizing $.when in node.js?

Is there a way to send multiple ajax requests simultaneously in node.js and have a single callback for all of them? I found a solution using jQuery here: Pass in an array of Deferreds to $.when(), but I'm not sure how to achieve this in node.js. Any s ...

Condensing a lengthy list by utilizing an array element

I require assistance, Looking at the code below, it is evident that I will have a lengthy list of code to handle an onblur and on focus scenario for a div and an input field. Is there a more efficient way to achieve this without writing separate lines of ...

Issue encountered during compilation in webpack when using the require() method with an imagePath

I'm currently attempting to dynamically load an image within a ReactJS project. Unfortunately, the solutions I've come across online have not been successful for me. Here is a snippet of my react component: class ReadOnlyTableRow extends React. ...

What is the impact of time-consuming computations on the performance of Vue server-side rendering?

Currently, I am in the process of developing a website for a major company that specializes in selling various goods. The website boasts an extensive catalogue with numerous search options and filters, which unfortunately take some time to compute and rend ...

AngularJS offers a convenient way for developers to fetch subcategories based on categories

Here is the JSON data provided for reference. JSON Data { "1":{"id":"1","name":"Websites IT & Software","sub_cat":[ {"id":"1","name":"Build a Website","description":"Build a Website"}, {"id":"2","name":"Build an Online Store","description":"B ...

Converting HTML images to jsPDF files

I need to print some HTML on a webpage using html2canvas and jsPDF. However, I am facing an issue where the images in the HTML are not getting printed. Here is my HTML and CSS code (full code available in fiddle): .handsomeHtml { background: red; } .c ...

Unraveling JSON string containing additional double quotes in Python

Does anyone know how to handle parsing a poorly formatted JSON String in python? Take a look at this example: "{""key1"":""value1"",""key2"":{""subkey1"":null,"&qu ...

Error: Attempting to access 'scrollTop' property of null object in Material UI library

After updating from MUI-4 to MUI-5, I encountered this error. Can anyone provide assistance? ...