Currently, I am working on a project in Ionic and facing an issue while attempting to read a JSON file locally. The process involves using a promise sent from a service which is then injected into a controller. Unfortunately, I keep encountering the error SyntaxError: Unexpected token J, where J always represents the first character of my JSON file.
Does anyone have any insights or solutions for this problem? Any help would be greatly appreciated!
Below is a snippet of my code within app.js:
.config(function($stateProvider, $urlRouterProvider) {
$stateProvider
.state('home', {
url: '/',
templateUrl: 'templates/home.html',
controller: 'MainCtrl as vm',
resolve: {
weather: function(MyService) {
return MyService.getData();
}
});
$urlRouterProvider.otherwise('/');
});
The corresponding code in services.js looks like this:
.factory('MyService', function($http) {
var base_url = "http://localhost:8100/";
function getData() {
return $http.get(base_url+'data/pt_br/mydata.json?callback=JSON_CALLBACK')
.then(
function(res) {
return res;
},
function(err) {
return err;
}
)
}
return { getData: getData() }
});
Here's how the controller is defined:
.controller('MainCtrl', function(weather) {
var vm = this;
vm.weather = weather;
});
Lastly, here is a glimpse of my json file:
JSON_CALLBACK ({
'Introduction': 'Welcome to my site!',
'texts' : [
'text1': 'hello',
'text2': 'world'
]
});