I am facing difficulty in accessing information from my API through Angular


I recently developed an API using NodeJS. While attempting to fetch data from the API using AngularJS, I encountered an issue where no data is being displayed.

Take a look at my API, which contains data in JSON format. You can access the live link to my API

{"status":"success","data":[{"_id":"58175cb6f0c48b2b9821d799","listen_url":"https://www.youtube.com/watch?v=XSbZidsgMfw","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Yonkers_tyler_cover.jpg/220px-Yonkers_tyler_cover.jpg","producers":"Tyler, The Creator","label":"XL Recordings","artist":"Tyler, The Creator","song_title":"Yonkers ","__v":0},{"_id":"581760e7aa7eb70a8cf0d770","listen_url":"https://vimeo.com/179791907","image_url":"https://upload.wikimedia.org/wikipedia/en/2/2a/Frank_Ocean_Blonde_2.jpg","producers":"Frank Ocean & Om'Mas Keith","label":"Boys Don't Cry","artist":"Frank Ocean","song_title":"Nikes","__v":0},{"_id":"582199b38e7a590871b38893","listen_url":"https://www.youtube.com/watch?v=x4pon-hdEXU","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/fa/Domo_Genesis-The_Alchemist-No_Idols.jpg/220px-Domo_Genesis-The_Alchemist-No_Idols.jpg","producers":"Alchemist","label":"Self Released","artist":"Domo Genesis","song_title":"Elimination Chamber  (featuring Earl Sweatshirt, Vince Staples & Action Bronson)","__v":0},{"_id":"58219ae58e7a590871b38894","listen_url":"https://soundcloud.com/defjam/frank-ocean-super-rich-kids","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/f/f5/Super_Rich_Kids.jpg/220px-Super_Rich_Kids.jpg","producers":"Malay","label":"Def Jam","artist":"Frank Ocean","song_title":"Super Rich Kids (featuring Earl Sweatshirt)","__v":0},{"_id":"58219c0a8e7a590871b38895","listen_url":"https://www.youtube.com/watch?v=0FcDXL5Aw0o","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/d/d0/Hive-earlsweat.jpg/220px-Hive-earlsweat.jpg","producers":"randomblackdude and Matt Martians","label":"Tan Cressida","artist":"Earl Sweatshirt","song_title":"Hive ( featuring Casey Veggies and Vince Staples)","__v":0},{"_id":"58219f3f8e7a590871b38896","listen_url":"https://vimeo.com/31781199","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/2/2e/Frank-Ocean-Thinkin-Bout-You-2012.png/220px-Frank-Ocean-Thinkin-Bout-You-2012.png","producers":"Shea Taylor","label":"Def Jam","artist":"Frank Ocean","song_title":"Thinkin Bout You","__v":0},{"_id":"5821a3e88e7a590871b38897","listen_url":"https://vimeo.com/49091270","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/7/71/Frankoceanpyramids.jpg/220px-Frankoceanpyramids.jpg","producers":"Frank Ocean, Malay and Om'Mas Keith","label":"Def Jam","artist":"Frank Ocean","song_title":"Pyramids","__v":0},{"_id":"582b4010b5a2eb460b850a2c","listen_url":"https://www.youtube.com/watch?v=FCbWLSZrZfw","image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/8/81/Chum_Earl_Sweatshirt.jpg/220px-Chum_Earl_Sweatshirt.jpg","producers":"randomblackdude, Christian Rich & Hugo (Outro)","label":"Tan Cressida","artist":"Earl Sweatshirt","song_title":"Chum","__v":0}]}

Here is my AngularJS code. I am attempting to set up a loop to display all the artists' names along with their song titles:

<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>


<div ng-app="myApp" ng-controller="customersCtrl">

<ul>
  <li ng-repeat="x in myData">
    {{ x.artist + ', ' + x.song_title}}
  </li>
</ul>

</div>

<script>
var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope, $http) {
  $http.get("https://forgetthis-jafar70.c9users.io").then(function (response) {
      $scope.myData = response.data.data;
  });
});
</script>

</body>
</html>

I am currently facing an issue and unable to troubleshoot what might be going wrong. Please advise.

Answer №1

Below your module declaration, make sure to include the following code:

app.config(['$sceDelegateProvider', function($sceDelegateProvider) {
         $sceDelegateProvider.resourceUrlWhitelist(['self', 'https://mysite-jafar70.c9users.io/**']);

}]);

Answer №2

Special shoutout to @Claies for helping me out with enabling CORS. I ended up incorporating these specific lines of code into my index.js file (nodejs).

server.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*')
    res.header('Access-Control-Allow-Methods','GET,PUT,POST,DELETE')
    res.header('Access-Control-Allow-Headers','Content-Type')
    next()
})

Answer №3

Initially, I encountered an error when attempting to access the API.

503 (Service Unavailable)

As a solution, I hardcoded the response in the controller :

var app = angular.module('myApp', []);
app.controller('customersCtrl', function($scope) {
  $scope.myData = [  
      {  
         "_id":"58175cb6f0c48b2b9821d799",
         "listen_url":"https://www.youtube.com/watch?v=XSbZidsgMfw",
         "image_url":"https://upload.wikimedia.org/wikipedia/en/thumb/4/41/Yonkers_tyler_cover.jpg/220px-  Yonkers_tyler_cover.jpg",
         "producers":"Tyler, The Creator",
         "label":"XL Recordings",
         "artist":"Tyler, The Creator",
         "song_title":"Yonkers ",
         "__v":0
      },
      {  
         "_id":"581760e7aa7eb70a8cf0d770",
         "listen_url":"https://vimeo.com/179791907",
         "image_url":"https://upload.wikimedia.org/wikipedia/en/2/2a/Frank_Ocean_Blonde_2.jpg",
         "producers":"Frank Ocean & Om'Mas Keith",
         "label":"Boys Don't Cry",
         "artist":"Frank Ocean",
         "song_title":"Nikes",
         "__v":0
      }, ...
    ];
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="customersCtrl">
<ul>
  <li ng-repeat="x in myData">
    {{ x.artist + ', ' + x.song_title}}
  </li>
</ul>
</div>

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

Retention of user-entered data when navigating away from a page in Angular

I need to find a way to keep the data entered in a form so that it remains visible in the fields even if the user navigates away from the page and then comes back. I attempted to use a solution from Stack Overflow, but unfortunately, it did not work as exp ...

Struggling to connect to Node/Express Backend

I have developed a backend service using Node and Express. However, I am encountering an issue where none of the routes for this application are being accessed. Despite expecting to see an error message in the console if the routes were misconfigured, no s ...

What methods can I use to retrieve traversed objects using riak-js?

Utilizing Node with riak-js to interact with a Riak database. I have established two buckets named invites and events. The invites bucket contains a link to events. Is there a way to fetch both the invite object and the corresponding event object in one qu ...

Searching for a specific collection item based on a custom property (excluding _id) in Meteor - what's the best approach?

I am facing an issue with my application that utilizes Flow Router along with its pub/sub functionality. I have set up a collection and template helpers. The code works fine on the client side. Template.theCase.helpers({ theCase: function () { ...

Utilizing JavascriptExecutor in Powershell

I've been working on a series of Powershell scripts that utilize the Selenium Webdriver. Now, I am looking to incorporate some JavaScript functionality into one of them. However, I am struggling to understand how to get the syntax right. I tried ada ...

Navigating through AngularJS routes with a Resolver

Can you explain the role of a route resolver in AngularJS? Is there a way to verify if the user is logged in by utilizing a $http Ajax request with the resolver before switching routes? ...

The third-party API is unable to access a class within a multi-module Maven project

Within my multi-module Maven project, I have included the JSON-IO dependency in the dao module. However, when attempting to deserialize my object, an error is thrown: Exception in thread "main" com.cedarsoftware.util.io.JsonIoException: Class listed in @t ...

Rotating an object in Three.js: Animate back and forth along two different azimuth angles

My three.js project features a 3D object that is meant to be viewed from the front only, as it appears as a single plane and is transparent from the back... Using orbitControls, I have restricted movement of both azimuth and polar angle... To enhance the ...

There was an issue parsing the data due to org.json.JSONException: The value 403, which is of type java.lang.Integer, cannot

I recently embarked on a journey to learn how to connect an Android app to a database by following a tutorial (https://www.youtube.com/watch?v=smVIDycK3-k).I diligently followed all the steps in the tutorial. However, upon running the app at the end, I enc ...

How can I generate spheres in threejs with varying radii while all passing through the shared point located at coordinates (0,0,200)?

Is there a way to generate spheres in threejs where the radius increases while always passing through a single point at 0,0,200? This would allow the origin of each new sphere to move along the z-axis. Appreciate any help, AriemWebgl ...

Difficulty dealing with Firestore using get() followed by add()

Recently started learning Vue.js and Firestore, facing a challenge with what should be a simple task. 1) I am trying to fetch default values from an existing template document in my Firestore database. 2) The goal is to use these default values to create ...

Looking to transfer JSON data between JavaScript and Java code

I am dealing with a JSON string within my JavaScript code and I want to pass this information to a Java class. Can I simply use a regular Java class for this task or is it necessary to implement a servlet? Furthermore, I am curious about the process of pa ...

A guide to implementing daily function calls for a week utilizing the @nestjs/scheduler module

Is there a way to schedule a function to run every day for a period of 7 days using Nestjs (@nestjs/scheduler)? @Cron(new Date(Date.now() + (24*60*60*1000) * 7) function() { console.log("This should get called each day during the next 7 days") ...

Can somebody please tell me the equivalent of the sleep() function in JavaScript?

Are there more efficient ways to implement a sleep function in JavaScript compared to the pausecomp function as shown below (sourced from here)? function pausecomp(millis) { var date = new Date(); var curDate = null; do { curDate = new Date(); ...

When the page is reloaded, the computed property in Vue.js (vuex) returns undefined due to the hardcoded array of objects data stored in vuex

I am facing an issue in my Vue shoe shop project where I am encountering 'undefined' error upon page reload while trying to access a getter through a computed property in the ShoeDetail component. My Vuex state contains hardcoded array of objects ...

The trustAsResourceUrl method is not defined and cannot be called in this context

Currently, I am utilizing Angular to showcase an iframe from YouTube. To achieve this, I implemented $sce to trust external URLs; however, I am facing an unresolved error. Can anyone provide some insights into this issue? Controller.js .controller(' ...

Utilizing onClick with material-ui button - functioning flawlessly for a single interaction

I have been attempting to encapsulate a Material-UI button within another component. Everything seems to be working well, except for when I try to handle the onClick event - it appears to only work once. Here is an example that demonstrates the issue: ht ...

Is PHP-generated HTML not responding to external Javascript?

I have been attempting to create a form where, upon checking a checkbox, the onClick() event will enable a select option. Below is my PHP code: function generateOption() { for($x = 0; $x < 10; $x++) { echo "<option value='".$x."'> ...

Exploring the power of Vue element manipulation

I'm diving into the world of web development and starting my journey with Vue on an online learning platform. Check out the code snippet below: <div id="app"> <form @submit.prevent="onSubmit"> <input v-model="userName"&g ...

Using jQuery and Bootstrap in an ASP.NET Core project

I am encountering an issue with the configuration of bootstrap and jquery within my project, causing these tools to fail to load properly. The problem seems to be that bootstrap is loading before jquery, resulting in error messages appearing when I check ...