0

I am building an Angular app. The computer I have is not connected to the Internet.

I tried to read a json file I created locally.

I tried with $http.get and I failed. Got cross origin error.

I read a little bit more and tried the JSONP request. I can see that i can now access to the json file, but the file is not read as I accepted.

It seems to me that the the browser doesn't understand to parse it as Json file.

What should I do?

Directive:

app.directive("Directive", ["Service", function(Service){
return {
restrict: "E",
templateUrl: "views/template.tpl.html",
link: function(scope, element, attributes){
scope.populateInfo = function(){
   var appData = Service.populateData();
    }
   }
  } 
}])

The Service:

app.service('Service', ['$http', function(http){
    this.populateData = function(){
    var url = "assets/mock.json";
    $http.jsonp(url).then(function(response){
        var result = response.data;

     });
    }
 }]);

I do not access to the .then. I got an error "Uncaught SyntaxError": Unexpected token :

mock.json:

    {
    "A": [
        {
           "name": "a",
           "id": 1
        },
        {
           "name": "b",
           "id": 2
        }
    ]
}
Aviade
  • 2,057
  • 4
  • 27
  • 49
  • "It seems to me that the the browser doesn't understand to parse it as Json file" — That isn't surprising since you told it to parse it as a JSONP file! – Quentin Nov 24 '15 at 08:11
  • @Quentin I was just going to post the same answer just the minute you commented though for reference, Please note that `.json` and `.jsonp` are 2 different things [more details](http://json-jsonp-tutorial.craic.com/index.html). – masterpreenz Nov 24 '15 at 08:17
  • You should to use a jsonp file like this: angular.callbacks._0( { "A": [ { "name": "a", "id": 1 }, { "name": "b", "id": 2 } ] } ) – Luis Nov 24 '15 at 08:22
  • I don't understand how to do the angular.callbacks._0. Can you demonstrate it please? – Aviade Nov 24 '15 at 08:29
  • Someone? I still can't read the file as json.... – Aviade Nov 24 '15 at 09:09
  • Quentin - not exactly a duplicate - as Aviade stated he wanted to 'I tried to read a json file I created locally.' So he can either a. use JSONP and remove browser security measures, or b. Serve the files to himself via http (using http-server, iis, node, tomcat, what-ever-can-serve-http server.) – Henry Aloni Nov 24 '15 at 12:12

0 Answers0