0

I have my sample data like this

var contacts = [{
    id: 0,
    'name': 'john',
        'email': '[email protected]',
        'phone': '123-2343-44'
}];

Instead using this way, how do i fetch the data from a json file using angularjs?

3
  • from a json file?. Where is the file located? Commented May 31, 2014 at 23:35
  • i exported the mysql data with php.localhost/contacts/all, this would be the link Commented May 31, 2014 at 23:40
  • then @mgnb has your answer. Commented May 31, 2014 at 23:41

2 Answers 2

6

Check out the $http service.

In short, if for instance the json endpoint is at the same web domain at /contacts_endpoint.json, include $http in your controller/service/whatever and run

angular.module('myModule').controller(function($scope, $http) {
  $http.get('/contacts_endpoint.json').success(function(contacts) {
    // the variable contacts contains the data you want 
  });
});
Sign up to request clarification or add additional context in comments.

1 Comment

this seems to working fine too, using this method, i am able to use the same variable name used in the rest of the program var url = 'http://localhost/contacts'; contacts = {}; contacts.getConacts = function() { return $http.get(url + '/all'); }
0

First of all, your JSON is not valid. Provided you have a valide one just write

var objectFromJSON = JSON.parse(validJSON);

and access any part of original JSON you want to.

3 Comments

i exported the mysql data with php.localhost/contacts/all, this would be the link
OK, I thought you wanted to know how to fetch something from the JSON instead of fetching a JSON itself. To do that just follow @mgnb's answer
ya, i wanted to fetch the entire data from a json file. thanks a lot

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.