1

I am new to Angular js and i wanted to pass one static json file through ajax and i am giving the codes below.How to Display the data of json file into the index file that i dont know. As i am new to angularjs please help me with these

Index.php

<body ng-app="Mymodule">
    <div ng-controller="recordscontroller">
        <table style="width:500px; border:solid 2px #CCC;">
            <thead>
                <tr style="text-align:left;">
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Gender</th>
                    <th>Salary</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="emp in employee | orderBy : '-salary'">
                    <td>{{ emp.firstname }}</td>
                    <td>{{ emp.lastname }}</td>
                    <td>{{ emp.gender }}</td>
                    <td>{{ emp.salary | currency: 'Rs' }}</td>
                </tr>
            </tbody>
        </table>
    </div> <br>
</body>

Script.js

var app = angular.module("Mymodule",[]);

    app.controller("recordscontroller", function($scope, $http){
        var url = "data/records.json";
        $http.get(url).success(function (response){
        $scope.employee = response;
        });
    });

records.json

[
    {
        "firstname":"Kishan",
        "lastname":"Dalsania",
        "gender":"Male",
        "salary":15000
    },
    {
        "firstname":"Dipesh",
        "lastname":"Mungara",
        "gender":"Male",
        "salary":20000
    },
    {
        "firstname":"Roshan",
        "lastname":"Trivedi",
        "gender":"Male",
        "salary":25000
    },
    {
        "firstname":"Jay",
        "lastname":"Dalsania",
        "gender":"Male",
        "salary":30000
    },
]
8
  • using objects we can send @Kishan Dalsania Commented Jan 4, 2017 at 12:10
  • How? Can you help me out by showing the code? just as an example. Commented Jan 4, 2017 at 12:11
  • ignore the syntax error in your json? pre-last line }, -> } Commented Jan 4, 2017 at 12:13
  • TypeError: dbg is undefined I am getting these type of error in console Commented Jan 4, 2017 at 12:14
  • jsfiddle.net/Mephiztopheles/qwd0y1pa wihout request, there is no error, except your syntax in your json file -- btw that seems not to cause the problem, i added the , too Commented Jan 4, 2017 at 12:15

1 Answer 1

1

Your JSON is not valid JSON.

If you check this version
https://jsfiddle.net/Mephiztopheles/qwd0y1pa
and this
https://jsfiddle.net/Mephiztopheles/qwd0y1pa/1/
JSON.parse('[{"firstname":"Kishan", "lastname":"Dalsania","gender":"Male","salary":15000},{ "firstname":"Dipesh", "lastname":"Mungara", "gender":"Male","salary":20000},{ "firstname":"Roshan", "lastname":"Trivedi", "gender":"Male", "salary":25000 }, { "firstname":"Jay", "lastname":"Dalsania", "gender":"Male", "salary":30000},]')

you will see

Sign up to request clarification or add additional context in comments.

2 Comments

Ok it worked. Thanks for help :) Is there any problem with request type? Without request it is working perfectly.
no, normally the xhtmlrequest detects which content-type the response is. javascript can parse the incorrect , but JSON doesn't

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.