Reading JSON from a URL in AngularJS using $http.get

wg4568

I've written a Flask back-end for an application I'm developing. In short, when calling http://localhost:5000/allsongs/ it returns something like (or very similar to) the following:

[["King And Lionheart", "Of Monsters And Men", "My Head Is An Animal", "mp3"], ["Just One Yesterday", "Fall Out Boy", "Save Rock And Roll", "mp3"], ["Laughter Lines", "Bastille", "All This Bad Blood", "mp3"]]

I'm trying to write a web-app using AngularJS that reads the data from that URL and uses ng-repeat to make a list that I can search through. So far, I have written this:

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

<html ng-app="myApp">

<body>

<script>
    angular.module('myApp', [])
        .controller('Songs', function Songs($scope, $http) {
    $http.get('http://localhost:5000/allmusic').then(function(response) {
      $scope.songs = response;
    });
  }
    );
</script>

<input placeholder="Search..." type="text" ng-model="search" />


<div ng-controller="Songs">
<p>{{songs}}</p>
</div>
<p ng-repeat="song in songs | filter:search | orderBy:name">{{ song }}</p>
</body>
</html>

All that shows up when I load the page is the search field at the top of the page.

Also, when I refresh the page I see my Flask server being called, so I know the $http.get is working somewhat.

Any advice would be great!

Egor Malkevich

It may be an CORS issue when you render HTML not at server side. So you have two options.

  1. Add some router that return index.html with all Angular code you need.
  2. Add some header response changes.

Sadly I have no experience with Flask, but at Node.js the second solution may looks like that

app.use((req,res)=>{
    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');

    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);
    req.next();
  });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get JSON object (as 'GET') from url using $resource in angularjs

From Dev

AngularJS - Reading JSON from Factory, using result

From Dev

AngularJS $http reading JSON data

From Dev

HTTP Status 0 from AngularJS Get for JSON

From Dev

Get value from URL using AngularJs

From Dev

Reading data from JSON file in Angularjs using a Promise

From Dev

Java: get json from http url (using JSON.org api)

From Dev

Java: get json from http url (using JSON.org api)

From Dev

Reading Json data using angularjs

From Dev

Reading Json data using angularjs

From Dev

Data is not get from json file using angularjs

From Dev

Reading URL from json file by using batch script

From Dev

Unable to read JSON in AngularJS $http from URL on local server

From Dev

Get the data from json url using php?

From Dev

Angularjs - Displaying data using $http get from remote server

From Dev

Reading data from JSON file in Angularjs

From Dev

Reading Json file from angularJs causing error

From Dev

Error in reading json response from http adapter

From Dev

Can't get JSON from remote server with AngularJS $http.json/get

From Dev

How get data from array in json item using angularjs

From Dev

How get data from array in json item using angularjs

From Dev

Get JSON data from PHP using Angularjs 2 Observable

From Dev

Not able to get the count of a specific ID from a json using angularjs

From Dev

Javascript reading JSON that comes from a url request

From Dev

Reading json content from a REST API URL

From Dev

angularjs call to an external url with $http.get

From Dev

Get JSON object from URL using HTTPURLConnection on android

From Dev

How to get json data from a url using flask in python

From Java

AngularJS: factory $http.get JSON file

Related Related

  1. 1

    How to get JSON object (as 'GET') from url using $resource in angularjs

  2. 2

    AngularJS - Reading JSON from Factory, using result

  3. 3

    AngularJS $http reading JSON data

  4. 4

    HTTP Status 0 from AngularJS Get for JSON

  5. 5

    Get value from URL using AngularJs

  6. 6

    Reading data from JSON file in Angularjs using a Promise

  7. 7

    Java: get json from http url (using JSON.org api)

  8. 8

    Java: get json from http url (using JSON.org api)

  9. 9

    Reading Json data using angularjs

  10. 10

    Reading Json data using angularjs

  11. 11

    Data is not get from json file using angularjs

  12. 12

    Reading URL from json file by using batch script

  13. 13

    Unable to read JSON in AngularJS $http from URL on local server

  14. 14

    Get the data from json url using php?

  15. 15

    Angularjs - Displaying data using $http get from remote server

  16. 16

    Reading data from JSON file in Angularjs

  17. 17

    Reading Json file from angularJs causing error

  18. 18

    Error in reading json response from http adapter

  19. 19

    Can't get JSON from remote server with AngularJS $http.json/get

  20. 20

    How get data from array in json item using angularjs

  21. 21

    How get data from array in json item using angularjs

  22. 22

    Get JSON data from PHP using Angularjs 2 Observable

  23. 23

    Not able to get the count of a specific ID from a json using angularjs

  24. 24

    Javascript reading JSON that comes from a url request

  25. 25

    Reading json content from a REST API URL

  26. 26

    angularjs call to an external url with $http.get

  27. 27

    Get JSON object from URL using HTTPURLConnection on android

  28. 28

    How to get json data from a url using flask in python

  29. 29

    AngularJS: factory $http.get JSON file

HotTag

Archive