1

The data in sakey-data.json file is array of arrays.

[
       [ 'Brazil', 'Portugal', 5 ],
       [ 'Brazil', 'France', 1 ],
       [ 'Brazil', 'Spain', 1 ],
       [ 'Brazil', 'England', 1 ]
]

I have been using this factory service to fetch the data.

app.factory('SankeyData', ['$http', function($http){

    return $http.get('http://localhost:4000/data/sakey-data.json')
        .success(function(data){
            return data;
        })
        .error(function(err){
            return err;
        });

}]);

It is unable to get data. Rather it shows this error.

SyntaxError: Unexpected token '

There is no syntax error at all. If I change the GET URL to actual JSON format file everything works well.

And moreover I can not change the format of this data in file because Google Graph API needs data in this format.

Please help me find a solution to fetch such files from server using AngularJS.

5
  • 8
    As per the specification. JSON strings have to be quoted in double quotes (") rather than single quotes ('). Commented Jun 3, 2015 at 7:48
  • Can you show your call to the SankeyData factory ? Actually you can't return data in a success like that. Commented Jun 3, 2015 at 7:59
  • You can return data "like that" in success but in this case only if transformResponse is overriden. Commented Jun 3, 2015 at 8:03
  • @MikkoViitala Sorry i didn't give enought explanation. I meant you can't return data "this way". The return function in .success will fire and ... return data to nothing. the .success handling the data shouldn't be in the factory. Commented Jun 3, 2015 at 8:08
  • Yes, true. Factory as it is, is not going to work but I assumed it's copy/paste error. Commented Jun 3, 2015 at 8:11

1 Answer 1

1

As state by @Phylogenesis you really should replace ' with ".
If that's not an option, then you can configure $http to transform the response.

angular.module('app',[])

  .controller('MainCtrl', function(SankeyData) {
    SankeyData.getData().then(function(data) {
      console.log(JSON.parse(data));
    });
  })

  .factory('SankeyData', function($q, $http) {
    var url = 'http://localhost:4000/data/sakey-data.json';
    return {
      getData: function() {
        return $http({ 
          url: url, 
          transformResponse: function(response) {
            return response.replace(/\'/g, '"');
          },
        }).then(function(response) {
          return response.data;
        });
      } 
    };
  });

imgur

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

3 Comments

will still be a string and JSON.parse won't convert it ...answer doesn't deal with how OP would convert to javascript array from string
Edited my answer to deal with it.
Down vote, eh? Probably from my personal, serial downvoter? No? Then please, leave a comment so I can improve my answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.