Fetching data from local JSON File in angularjs - json

I want to fetch data from JSON file which is on my local machine. But I am not able to get the data. It is showing some cross domain error for $http.
Here is my code.
angular.module('myApp',[])
.controller('myCtrl', function ($scope, webtest) {
webtest.fetch().then(function (data) {
$scope.accounttype = data;
})
});
.factory('webtest', function($q, $timeout, $http) {
var Webtest = {
fetch: function(callback) {
return $timeout(function() {
return $http.get('webtest.json')
.then(function(response) {
return response.data;
});
}, 30);
}
};
return Webtest;
});
Anyone please help me how to display data from local JSON file?
Thanks in Advance.

It's very simple like
$http.get('phones/phones.json').then(function(response) {
$scope.phones = response.data;
});
Refer:http://stackoverflow.com/questions/21589340/read-local-file-in-angularjs

Don't you have an error message like "$http: is not defined" ?
I tried with a controller, this is working :
var ngApp = angular.module("ngApp", []);
ngApp.controller('myController', ['$http', function($http){
var thisCtrl = this;
this.getData = function () {
this.route = 'webtest.json';
$http.get(thisCtrl.route)
.success(function(data){
console.log(data);
})
.error(function(data){
console.log("Error getting data from " + thisCtrl.route);
});
}
}]);
If you haven't, use web developer tools (Ctrl+Shift+I in firefox).

If you haven't already done so. Try setting up a crossdomain policy for your application.

Related

AngularJS Cannot read property 'getData' of undefined in VS

do you know what I am doing wrong? I want to read data from my json-file but i got the error that it canĀ“t read the property getData.
myApp.service('jsonDataService', function ($http) {
this.getData = function () {
return $http({
method: 'GET',
url: '/jsonData/Stations.json'
});
}
});
controller:
myApp.controller('IndexController', ['$scope', function ($scope, jsonDataService) {
jsonDataService.getData().then(function (msg) {
$scope.msg = msg;
console.log(msg);
});
}]);
I am using ng in Visual studio in a mvc project.
path json-file: " Visual Studio 2015\Projects\Test\WebApplication\Scripts\jsonData\Stations.json"
In the controller code which you have shared, you have not injected 'jsonDataService' service properly.
It should be:
myApp.controller('IndexController', ['$scope', 'jsonDataService', function ($scope, jsonDataService) {
jsonDataService.getData().then(function (msg) {
$scope.msg = msg;
console.log(msg);
});
}]);

Getting data from multiple json in angular js

How can I get the data from two JSON files, named gridData.json and AddedData.json simultaneously using $http.get function?
PApp.controller('ProjectDataController', function($scope, $http) {
$scope.addProject=function($scope){
};
$scope.getData = function(){
$http.get('AddedProjects.json').success(function(data) {
$scope.ProjectStat = data;
});
$http.get('JSON/gridData.json').success(function(data) {
$scope.ProjectStat = data;
});
};
});
Better is to use two seperate variables on the scope. But if you have your reasons to keep it the same objects. You can merge them like below.
PApp.controller('ProjectDataController', function($scope, $http) {
$scope.ProjectStat = {};
$scope.addProject=function($scope){
};
$scope.getData = function(){
$http.get('AddedProjects.json').success(function(data) {
angular.extend($scope.ProjectStat, data);
});
$http.get('JSON/gridData.json').success(function(data) {
angular.extend($scope.ProjectStat, data);
});
};
});

Ionic reading json file from web

After trying what this question ask, I'm asking again how to retrieve data from web using AngularJS in the Ionic framework.
Basically, I do what the answer says:
.factory('Advices', function($http) {
var advices = null;
$http.get('http://myurl.myext/myfile.json').success(function (data) {
advices = data;
}).error(function(error) {
console.log('error'); //even if there i print the error it prints nothing
});
//etcetera
How can I rescue that file from my server?
Please try to following code
var app = angular.module('myApp', ['ionic']);
app.controller('mainInfoFactory', ['$scope', '$http', function($scope,$http) {
$http.get("http://myurl.myext/myfile.json")
.success(function (response)
{
$scope.advices = response;
})
.error(function(data) {
alert("ERROR");
});
}]);
Here myAPP and mainInfoFactory are AngularJS application name and controller respectively.
For example : ng-app="myApp" ng-controller="mainInfoFactory"

AngularJS : Factory JSON Array with HTTP GET

I'm developing my first AngularJS app using the Google Docs API to pass it JSON data.
This is an example of the factory I'm using:
app.factory('Data', ['$http', 'apiKeys', function($http, apiKeys){
var googleDocs = 'https://spreadsheets.google.com/feeds/list/';
return {
news:function () {
return $http.get(googleDocs + apiKeys.googleDoc +'/1/public/values?alt=json', {cache: true});
},
updates:function () {
return $http.get(googleDocs + apiKeys.googleDoc +'/2/public/values?alt=json', {cache: true});
},
docs:function () {
return $http.get(googleDocs + apiKeys.googleDoc +'/3/public/values?alt=json', {cache: true});
}
}]);
I wanted to clean up a bit the code and decided to use services instead of making the calls in the controller itself. It works normally, but it's a pain in the ass the fact that I still need to write long $scopes because of the structure of the Google API. This is how I get the values in the controller:
app.controller('homeCt', ['$scope', 'Data', function ($scope, Data){
Data.news().success(function (data) {
$scope.totalNews = data.feed.entry.length;
});
}]);
Is there a way that I can set the factory service to pass me the data just using:
$scope.totalNews = Data.news()
Or at least removing the 'feed.entry'?
Data.news().success(function (data) {
$scope.totalNews = data.length;
});
Thank you very much!
example of service - resolve the success with the data you want
app.service('Data', ['$http', 'apiKeys', function($http, apiKeys){
var googleDocs = 'https://spreadsheets.google.com/feeds/list/';
this.news =function(){
return $http.get(googleDocs + apiKeys.googleDoc +'/1/public/values? alt=json', {cache: true})
.then(function(data){
return data.feed.entry.length;
});
}
}]);
the controller - since you already resolved the data in service hence..
app.controller('homeCt', ['$scope', 'Data', function ($scope, Data){
Data.news().then(function (data) {
$scope.totalNews = data;
});
}]);
working example
var app = angular.module('app', ['ionic'])
.service('Data', ['$http',
function($http) {
var googleDocs = 'https://spreadsheets.google.com/feeds/list/1aC1lUSxKatfxMKEy1erKDSAKgijSWOh77FDvKWhpwfg/1/public/values?alt=json';
this.news = function() {
return $http.get(googleDocs, {
cache: true
}).then(function(res) {
return res.data.feed.entry;
});
}
}
])
.controller('homeCt', ['$scope', 'Data',
function($scope, Data) {
Data.news().then(function(data) {
console.log(data);
})
}
]);
I'll give you a way of doing it, a way that I don't recommend at all (a service should not handle the scope), but for me it is the only way you have if you don't want to destroy the "async" of your ajax call :
app.factory('Data', ['$http', 'apiKeys', function($http, apiKeys){
var googleDocs = 'https://spreadsheets.google.com/feeds/list/';
return {
news:news,
updates: updates,
[...]
}
function news(scopeValue) {
$http.get(googleDocs + apiKeys.googleDoc +'/1/public/values?alt=json', {cache: true}).success(function(data){
scopeValue = data;
});
}]);
and then, call it that way in your controller :
Data.news($scope.totalNews);

Make AngularJS service call async

I have read about "promise" object and all the ways to get some sort of async call or wait until a http call is done, but I haven't been able to success. This is what I got and what I'm trying to do:
I need to get some json file from my server and use the data from that json in my code (js file) and not only as data for my HTML template.
I have a service that does the call to the json file:
mobilityApp.service('serveiWebservices', function($resource) {
return {
getWS: function($scope) {
var path = 'jsonWS/context.json';
$resource(path).get(function (data) {
console.log(data); //data is printed fine
$scope.returnData = data; //just to test, it doesn't work neither
return data;
});
}
};
});
And from my controler I call it like this:
var data = serveiWebservices.getWS($scope);
console.log(data); //undefined
any idea on how to work with the promise object that the funcion returns and perform actions as soon as it gets the requested data ? i Know that I could set a "success" function but I would like not to use callbacks.
Tnanks in advance !
This should work -
Service:
mobilityApp.service('serveiWebservices', function($http) {
return {
getWS: function() {
var path = 'jsonWS/context.json';
return $http.get(path, function (response) {
console.log(JSON.stringify(response, null, 4));
return response.data;
});
}
};
});
Controller:
serveiWebservices.getWS().then(function(data) {
console.log(JSON.stringify(data, null, 4));
});
If you want to use $resource this should work too -
mobilityApp.service('serveiWebservices', function($resource) {
return {
getWS: function() {
var path = 'jsonWS/context.json';
return $resource(path).get(function (response) {
console.log(JSON.stringify(response, null, 4));
return response; // might just be response, no response.data
});
}
};
});
I was searching for a working solution from hours. thanks #Ross.
This also work , i modified the Ross example , and removed the first return :
mobilityApp.service('serveiWebservices', function($http) {
this.getWS = function() {
var path = 'jsonWS/context.json';
return $http.get(path, function (response) {
console.log(JSON.stringify(response, null, 4));
return response.data;
});
}
this.getWS2 = function() {
var path = 'jsonWS2/context.json';
return $http.get(path, function (response) {
console.log(JSON.stringify(response, null, 4));
return response.data;
});
}
});
If i get a lot of function in this service should i use the Ross example with all the function in the return or this one ? thanks