The challenge of reading a local JSON file in Angular JS

I previously had success with my query in a one-page sample project (index.html), but now I am working on a project with tabs (I created the project using Ionic Lab with tabs). I need to display the output of a JSON file on the tab-home.html page, which is not in the same location as the JS and index.html files.

The tab pages are located in the /Template folder (tabs.html, tab-home.html, tab-account.html) while the JS files are in the /JS folder (app.js, controllers.js, services.js, and employees.json).

Here are my codes:

In controllers.js

.controller('HomeCtrl', function($scope, webtest) {
    webtest.fetch().then(function(data) {
        $scope.data = data;
    })
})

In services.js

.factory('webtest', function($q, $timeout, $http) {
    var Webtest = {
        fetch: function(callback) {
            return $timeout(function() {
                return $http.get('employees.json').then(function(response) {
                    return response.data;
                });
            }, 30);
        }
   };

   return Webtest;
});

In tab-home.html

<ion-item class="item-remove-animate item-avatar item-icon-right" ng-repeat="item in data">

    <h2>{{item.product_name}}</h2>   
</ion-item>

I haven't made any changes to app.js and index.html. They remain with default code.

Thank you all, Mehmet.

Answer №1

please ensure your reply follows the format {"information": {....

in case of absence:

 utilize $http.get('staff.json').then(function(response) {
                return response;
            });

Answer №2

Trying to access files directly from your hard drive? If the URL in the address bar starts with file://, AJAX won't function properly. You'll need to run them on a web server instead. WAMP is a great option for setting up a local web server on Windows operating systems. You can find more information about it here:

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

Submitting Forms with XMLHttpRequest

I'm trying to utilize XMLHttpRequest in order to retrieve the response from a remote PHP file and showcase it on my webpage. The issue I'm facing is that the form's data isn't being submitted to the remote PHP page. Any suggestions on ...

Error in GraphQL query: specified argument is mandatory, yet not supplied

I recently started learning about graphql and encountered an issue with my query. Here is the code I am using: { product { id } } "message": "Field "product" argument "id" of type "String!" is requir ...

What are the steps to install node.js on hosting servers like Hostinger, JustHost, and others?

Recently, I've been diving into the world of Node.js. While I have some experience with PHP, I've found that most hosting services already have a PHP interpreter installed, making it easy to work with. However, I'm now trying to figure out i ...

Utilizing clip-path polygons for effective styling on Firefox and iOS

I have been working on a plugin to create animated modal boxes by utilizing the clip-path property. However, I have encountered an issue where this code only seems to work in Chrome. You can view the codepen demo here. Unfortunately, it appears that Firef ...

Clickable boxes for selecting options (Ajax and JavaScript)

I have a new project that involves creating a checkbox and using ajax and javascript to change its state when clicked. The goal is for the checkbox state to be saved in a php program that generates a .txt file. The state should be 0 when not clicked and 1 ...

The mystery behind why including a [ ] in the json string suddenly made it work, after failing to do so previously

Here is the JSON string that was originally stored in a .txt file: {"SerialNumber":"1","Festival":"New Year","Day":"Friday"} When attempting to access this data from the code behind (C#), I encountered an error when deserializing the JSON string into a g ...

Update AngularJS from version 1.2.6 to Angular 16 in order to resolve the Material issue

Currently, I am working on a project that involves upgrading AngularJS version 1.2.6 to Angular 16. However, I have encountered a significant challenge in understanding the variances between AngularJS material and Angular material. For example, in Angular ...

What is the process of calculating the average of JSON data and looping through it multiple times using Javascript?

I've encountered a JSON data set that has the following structure: { name: "Evelyn", assignment: "SCRUM", difficulty: 3, fun: 4 } And so on. My goal is to calculate the average value of both difficulty and fun for e ...

What causes the disparity in height between a textbox and the encompassing span element?

What causes the discrepancy in element heights when looking at the code below? <span id="spanner" style="margin:0;padding:0;border:0;outline:0;"><input type="text" /></span> If you check the height of the text box, it will be shown as 1 ...

Using VueJS to perform a filtering operation on the JSON data when a button is clicked

I need to implement a filter function for my fetched json data using buttons. When a button is clicked, only the data (in this case book names) that match the clicked button should be displayed while hiding the others until another button is clicked. The ...

Tips on extracting values from the nested array using react axios

While the renderQuestions section works fine and prints the first question, there is an issue with the renderChoices part. The desired output is to display the choices for the first question as (a.)1970 b.) 1971 c.)1972 d.)1973) but it currently displays ...

Using JavaScript, you can add an article and section to your webpage

Currently, I am utilizing AJAX to showcase posts. My objective is to extract each property from a JSON object that is returned and generate an <article> for the title followed by a subsequent <section> for the content. While I can successfully ...

Utilizing ajax to transfer local storage data to a controller

I am looking to access the information stored in localstorage on the server side, but that's a story for another time. Within my view, I have this button: <div style="float:right;"> <input id="btnTest" type="button" name="test Json Butt ...

AngularJS does not provide access to child scope forms within the parent scope when using the ng-switch directive

How can I access the AT model object written in ng-switch? When using ng-switch, a child scope is created and the form exists on this scope. This means that the child scope form will not be available on the parent scope. Can someone please assist me with ...

Supervising the organization of HTML sections for an offline web application

Currently, I am developing an offline HTML5 application that involves a significant amount of DOM manipulation through the creation of HTML strings within JavaScript functions. For example: var html=''; html+='<div class="main">&apos ...

The refresh method cannot be found in 'x'. (In the context of 'x()', 'x' represents an Object instance)

Is there a way for me to access the refresh() method within my UpdateLokalListe function? Can I integrate the function into my class? I followed the instructions in this guide: https://reactnavigation.org/docs/function-after-focusing-screen Thank you ht ...

Unexpected issue with AJAX call: browser error caused by prolonged script execution

I'm encountering an issue with my ajax call where the browser throws a "not responding due to a long-script is running" message and provides a button to stop the script. Although the URL is being fetched correctly, the console.log(4) within the succe ...

The Vue Router hooks are not being activated within the component when utilizing Typescript

I've been pondering this issue for quite some time. Despite my extensive search efforts, I am still unable to figure out why the route-hooks are not working in my component: 1. The component is being loaded from RouterView: <router-view class="z1 ...

Encountered an error while attempting a GET request to 'http://localhost:3000/': The resource failed to load, resulting in a net::ERR_CONNECTION_REFUSED error in delete.js

Every time I attempt to GET '/' through my express server, I encounter an error message indicating "Failed to load resource: net::ERR_CONNECTION_REFUSED ... delete.js". This issue typically arises when I try to submit a form from http://localhost ...

How can the value of a button be displayed in an input box using an onclick function?

When the button '1' is clicked, it displays the value "1" inside a <!p> tag. However, the second button '2' does not display the value "2" in the input box. Even though both functions are identical. //Function with working func ...