Unable to retrieve information from JSON file utilizing AngularJS version 1.6

I'm having trouble retrieving data from my JSON file using AngularJs 1.6

  myApp.controller("homeCtrl", function($scope, $http) {
      $scope.Data = [];
      var getJsonData = function() {
          $http.get('contactlist.json').then(function(response) {
              $scope.Data = response.data;
              console.log(response.data);
          });
      }
      getJsonData();
  });

The issue I'm encountering is that the code doesn't seem to be reaching the response part and my page loads without stopping at the debug response. It seems stuck on `then(function(reponse){`

This is how my JSON File looks like:

var contactList = [
{
"firstName": "Joe",
"lastName": "Perry",
"contactNumber": "444-888-1223",
"contactEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8ce6e3e9ccefe3fee8e5ffa2f9ff">[email protected]</a>"
},
{
"firstName": "Kate",
"lastName": "Will",
"contactNumber": "244-838-1213",
"contactEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325953465772515d40565b411c4741">[email protected]</a>"
}
];

Answer №1

Situation resolved successfully. The issue stemmed from a semicolon at the conclusion of the JSON file data. This error surfaced upon attempting to paste it into the Plunker editor. Mea culpa.

Answer №2

Modify your JSON file (as it is currently invalid):

[{"firstName":"Joe","lastName":"Perry","contactNumber":"444-888-1223","contactEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b5dfdad0f5d6dac7d1dcc69bc0c6">[email protected]</a>"},{"firstName":"Kate","lastName":"Will","contactNumber":"244-838-1213","contactEmail":"<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e289839687a2818d9099886fc797d08d091de">[email protected]</a>"}]

Answer №3

Do not include var contactList = in your JSON file; only place the JSON contents itself.

For example:

[
   {
        "firstName": "Joe",
        "lastName": "Perry",
        "contactNumber": "444-888-1223",
        "contactEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="92f8fdf7d2f1fde0f6fbe1bce7e1">[email protected]</a>"
    },
    {
        "firstName": "Kate",
        "lastName": "Will",
        "contactNumber": "244-838-1213",
        "contactEmail": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1f74 7e6b7a5f7c706d7b766c316a6c">[email protected]</a>"
    }
]

var contactList = <something>
implies that it's JavaScript code meant to be executed. However, if you are reading the file and parsing it as JSON data rather than executing it as a js file, ensure that the contents are structured as a JSON string and not JavaScript code.

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 TableView appears to be selectively skipping over certain cells in order to present data sourced from

After successfully parsing the JSON data, I faced an issue when trying to display it in a TableView. The problem was that only certain rows were being displayed, such as the first row followed by the sixth and so on. You can see the issue here: This is ho ...

Loop through options in Vue.js and set a specific option as selected

When iterating through a list of objects, I am struggling to set the status of each object as selected by default. <template> <table class="table is-striped"> <thead> <tr> <th> ...

javascript variable pointing to an unknown object

Below is the essential code snippet to consider: JS function ToggleRow(objTwistie, objRow) { if (objRow.style.display == "none") { objRow.style.display = ""; objTwistie.src = "../Images/SectionDown.gif"; } else { objRow.style.display = "n ...

What is the best way to loop through the contents of this JSON document?

I am looking to create my resume using React. I have a JSON file that includes all my work experience, education, and more. However, I am facing an issue with the way the data is displayed. { "experience":[ { "Title":"Previous Job ...

Error: React-Redux unable to locate Redux context value while utilizing useSelector function

After creating a store and authreducer, everything was working as expected. However, when I added the useSelector in app.js, an error occurred: ERROR Error: could not find react-redux context value; please ensure the component is wrapped in a <Provid ...

Column Locking with Merged Rows

I have implemented row spanning in jqgrid by following the instructions provided in this answer: Jqgrid - grouping row level data However, I am facing an issue where setting a column with row span to frozen = true causes the overlay to lose the row spanni ...

How can I utilize the mapping function on a promise received from fetch and display it on the page using React

As I'm using fetch to return a promise, everything is working fine. However, I am facing an issue while trying to map over the fetched data. When I check my console log, it shows "undefined." const dataPromise = fetch('http://api.tvmaze.com/sche ...

modifying the source of an Ajax POST request

Is it possible to modify the referrer in an HTTP Ajax call using jQuery or JavaScript? I'm looking to send a request from my page but have the referrer appear as though it came from another page. Any insights would be appreciated. ...

npm: generate new script directive

When I start up my NodeJs (ES6) project, I usually enter the following command in the console: ./node_modules/babel/bin/babel-node.js index.js However, I wanted to streamline this process by adding the command to the scripts section of my package.json fi ...

Refreshing HTML content using event delegation in JavaScript

Currently, I am attempting to dynamically load additional HTML content onto my webpage when a button is clicked. Here is the code that I have implemented so far: Main HTML Code: <div class="row" id="gallery-wrapper" > <di ...

Combining multiple if conditions in jq when keys and values are sourced from disparate files

Within the JSON file license.json, I have two keys located at the bottom - "visitsAnnualQuota" and "visitsQuota": { "customMetricsLimit": 9223372036854776000, "customMetricsOverageLimit": 9223372036854776000, ...

Personalize the jquery autocomplete outcome

I'm currently utilizing jQuery autocomplete along with a remote data source. $( "input#searchbar" ).autocomplete({ source: function( request, response ) { $.ajax({type: "post", mode: "abort", dataType: ...

Passing parameters from a div to a single page component in Vue.js: A complete guide

There is code that appears on multiple pages: <div id="contact-us" class="section md-padding bg-grey"> <div id="contact"></div> <script src="/dist/build.js"></script> </div> Included in main.js is: im ...

The ASP.net WebMethod seems to be providing a full page output instead of the expected JSON data

I am implementing an auto complete functionality for a textbox. However, when I send a GET request to a web method, instead of receiving the actual data, I get the entire page content. Here is the C# code snippet that I am using: [WebMethod] [Scr ...

Error: 'require' function is not recognized in the latest JavaScript file

I am interested in using anime.js. To install this library, I need to follow these steps: $ npm install animejs --save const anime = require('animejs'); The code should be written in a js file named "anime.js" Here is the code for "anime.js": ...

Is it possible to generate multiple services from a single factory in Angular?

Recently, I developed a service called tableService using the following code: app.factory('tableService', [function() { var data = some data; var foo = function (){ //do something to data here }; return { data: data, ...

What is the best way to extract information from a JSON file and display it on a webpage using

I am new to this and I have a question for everyone Here's an example of the JSON response from my URL: The JSON data returned is as follows: { "Data":{ "id": 1312, "Name": "Steem Dollars", "Symbol": "SBD", "website_slug": "steem-dollars", "Level": ...

Encountering the 'spawn gulp ENOENT' error with Yeoman/Gulp setup on a Mac machine

Just a heads up: While browsing, I did come across this particular question and also this one, but those situations were both related to Windows. The solutions provided were specific to Windows, whereas I am using a Mac! Please refrain from marking this as ...

"Unable to move past the initial segment due to an ongoing

My portfolio webpage includes a "blob" and "blur" effect inspired by this YouTube video (https://www.youtube.com/watch?v=kySGqoU7X-s&t=46s). However, I am encountering an issue where the effect is only displayed in the first section of the page. Even a ...

Tips for converting a date string to a date object and then back to a string in the same format

I seem to be encountering an issue with dates (shocker!), and I could really use some assistance. Allow me to outline the steps I have been taking. Side note: The "datepipe" mentioned here is actually the DatePipe library from Angular. var date = new Dat ...