1

I am new to angular and trying to integrate it within my application. I am attempting to use a simple $http.get to a .JSON file, which seems to be found, but when trying to pass the data to the front end, or even alert it, is it alerting as []

Here my get:

$scope.countries = [];
    $http.get('/resources/data/countries-report.json', function(data){
            $scope.countries = data;
        }, function(error) {
            //handle error here
    });

    alert(JSON.stringify($scope.countries));

Here's my .JSON file:

{
  "firstName"   : "Joe",
  "surName" : "Bloggs",
  "countries" : [
      { "name": "France", "population": "63.1" },
      { "name": "Span", "population": "52.3" },
      { "name": "United Kingdom", "population": "61.8" }
  ]
}
3
  • 1
    The $http.get is asynchronous and hasn't completed yet. If you put a $watch on 'countries', the callback will run when $scope.countries is updated. You could also use {{countries}} in one of your views and you'll see the data in the view when it returns. Commented Apr 17, 2014 at 14:25
  • @EthanJewett - sorry, where should $watch go? Commented Apr 17, 2014 at 14:27
  • Anywhere in your controller. The other answers are correct as well. I would suggest logging in your success function before you assign data to $scope.countries, as it sounds from your responses to other answers like you're never getting to this point. Commented Apr 17, 2014 at 16:15

3 Answers 3

2

Try this:

$scope.countries = [];
$http.get('/resources/data/countries-report.json', function(data){
        $scope.countries = data;
alert(JSON.stringify($scope.countries));
    }, function(error) {
        //handle error here
});

Alert should go in the callback.

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

10 Comments

Maybe like this : $http.get('/resources/data/countries-report.json') .success(function(data) { alert(data); }.error(function() { console.error("$http.get() error"); }
Tried: $http.get('/resources/data/countries-report.json').success(function(data) { alert(data); }); and an alert appears with [object Object]
@ppa Yes, it's work So try to alert(JSON.stringify(data)); or just console.log(data), you will see your data in console.
Into success function ? huum strange :/ I didn't see before, but if data is on json format, you must use JSON.parse(data), not stringify. I don't know if it will solve your problem, but maybe console.log(JSON.parse(data)) will work ?
@ppa the code in the answer is not correct. Should be $http.get(...).success(successCallback).error(errorCallback) or $http.get(...).then(successCallback, errorCallback) see docs: docs.angularjs.org/api/ng/service/$http#get
|
2

This should do it

$http.get('/resources/data/countries-report.json').success(function(data) {
    $scope.countries = data;
    alert(JSON.stringify($scope.countries));
  }).error(function(error) {
    alert('error');
  });

also this equivalent would work

$http.get('/resources/data/countries-report.json').then(function(data) {
    $scope.countries = data;
    alert(JSON.stringify($scope.countries));
  }, function(error) {
    alert('error');
  });

Edit --

  1. Instead of $http.get('/someUrl', sucessCallback, errorCallback) it should be either

    $http.get('/someUrl').success(successCallback).error(errorCallback) 
    

    or

    $http.get('/someUrl').then(successCallback, errorCallback) 
    

    see docs

  2. Because $http.get is asynchronous alert should be moved in the successCallback function, to ensure that $scope.countries=data is executed before calling alert.

3 Comments

im not sure how this helps?
I was testing the code in a plunker first and forgot to change the url of the json file back to yours when I posted. I just re-edited accordingly.
OK you got it working, just edited to clarify my answer anyway. Hope it helps.
1

$http.get is an async function. when you try alert(JSON.stringify($scope.countries)); $scope.countries is equals to []. You must wait for server response before using data.

Try to do your alert into success function

$scope.countries = [];
$http.get('/resources/data/countries-report.json', function(data){
        $scope.countries = data;
        alert(JSON.stringify($scope.countries));
    }, function(error) {
        //handle error here
});

1 Comment

@Mysuno - Tried the above, even added console.log("console = " + JSON.stringify($scope.countries)); but the alert or console message never displays.. But in Firebug, i can see a successful GET to the JSON file and the data inside.

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.