Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display json data on html page using angularjs?

I am trying to create a small angular that takes a json file and displays the data on an html page. However I am getting the following error: POST bookmarks/data/bookmarks.json 405 (Method Not Allowed).

Here is my code:

<html lang="en" ng-app="bookmarksApp" id="ng-app">
    <head>
        <link rel="stylesheet" href="css/styles.css" />
    </head>
    <body ng-controller="BookmarkController as bookmarksCtrl">
        <article class="main-content" role="main">
            <section class="row">
                <div class="content">
                    <div class="bookmarks-list">
                        <h1>Christine's Bookmarks</h1>
                        <ul>
                            <li ng-repeat="bookmark in bookmarksCtrl.bookmarks" class="">
                                <h3>{{bookmark.name}}</h3>
                                <a href="{{bookmark.url}}">{{bookmark.url}}</a>
                            </li>
                        </ul>
                    </div>
                </div>
            </section>
        </article>
        <script type="text/javascript" src="scripts/angular.min.js"></script>
        <script type="text/javascript" src="scripts/main.js"></script>
    </body>
</html>

and my angular code:

(function() {

    var app = angular.module('bookmarksApp', []);   

    app.controller('BookmarkController', function($scope, $http) {

        $http({ method: 'POST', url: 'data/bookmarks.json' }).success(function (data) {
            console.log('hello');
            $scope.bookmarks = data; // response data 
        }).
        error(function (data) {
            console.log(data);
        });
    });

})();

Any ideas where I am going wrong? Thanks.

like image 883
cm381 Avatar asked Nov 01 '22 21:11

cm381


1 Answers

Try to change the POST to GET.

Also fix the problem in ng-repeat

use

<li ng-repeat="bookmark in bookmarks" class="">

at the place of

<li ng-repeat="bookmark in bookmarksCtrl.bookmarks" class="">
like image 53
Mritunjay Avatar answered Nov 09 '22 09:11

Mritunjay