2

I'm new to Angular and also relatively new to the JSONP format. In my code, I set up a factory to read in the JSONP file - and I get back the data variable as the JSONP data properly

$http.get('someJSONFile').success(function(data){
    console.log(data);
}); 

The console log gives me back the following:

states([{"code":"AL","name":"Alabama"},{"code":"AK","name":"Alaska"},{"code":"AZ","name":"Arizona"},{"code":"AR","name":"Arkansas"},{"code":"CA","name":"California"},{"code":"CO","name":"Colorado"},{"code":"CT","name":"Connecticut"},{"code":"DE","name":"Delaware"}])

So - I'm stuck on what to do now. I have a data object that contains a function. I need to parse out the JSON from this function - but not sure how to do it.

I have read in other places that I need to declare a function somewhere in my controller or factory called

function states(results)
{
    // what is in results should be the JSON data I crave
}

But even if I do that, I don't think it ever executes - so I am not sure what I should do at that point to get the JSON data into an object that I can use.

The full file I am trying to parse is http://massachusettswebdesigns.com/states.json

Any help is appreciated!

1
  • Yeah, I think you can just call try {eval(data) } catch (e) {} and then in your states function, throw in console.log(results) to see what happens. Commented Apr 2, 2015 at 20:03

3 Answers 3

1

Angular $http service provides a method to consume JSONP endpoints. In your case this should work:

$http.jsonp('someJSONFile').then(function(data) {
    console.log(data)
});

Behind the scene, Angular will create a globally accessible function that will receive data, parse and pass further, so you don't need to worry about creating functions yourself.

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

4 Comments

OK, now I am getting an error that states is not defined
Is it you own service? It is not really JSONP, you should not hard code states function in response. Instead you should take whatever GET parameter for callback comes from client and wrap JSON data with it.
No - not my service, and thats the problem.
Is there maybe a way to say that you want to use different function? Is this service is configurable? I checked it doesn't understand GET parameter callback, maybe different one? Otherwise you will have to create global function states.
0

What looks like your trying to do is standard JSON not JSONP, JSONP will require a callback and is only required if getting data from a different domain, ie a API service

Also the console log output you give is not valid JSON.

//init app
var app = angular.module('MyApp',[]);

//setup a factory for the data handling

app.factory("Data",function($http){
return {

   getData:function(type){

   return $http.get(type).
       then(function(result) {
           return result.data;
       });
   }
}
});

//in a controller 'call' the factory for the data

app.controller('main', function main($scope, Data) {

//other stuff

Data.getData('someJSONFile').then(function (data) {
    $scope.jsonData = JSON.parse(data);
});

//other stuff
});

2 Comments

OK, this got me to a part where I got a No 'Access-Control-Allow-Origin' header is present on the requested resource, error. I was able to get around that by using a plugin for Chrome that allows me to overwrite the cross origin error. But now when I parse the file I get an SyntaxError: Unexpected token s error when it trys to parse the state. The file I am trying to parse is massachusettswebdesigns.com/states.json
Depending on the data format you may not need to parse it, ie if not a string, so change the line $scope.jsonData = JSON.parse(data); to $scope.jsonData = data;
0

So my error was a stupid one. The JSON file was giving me the name of a custom callback I had to define. Once I did this, all worked. . . . here's the code

angular.module('locationApp')
  .controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {


    $scope.states = [];
    $scope.cities = [];


    // Function for the States callback on the JSON files provided
    window.states = function (data)
    {
      $scope.states = data;
    };

     // Get the State JSON file.   
    $http.jsonp('http://locationinc-mapping-demo.herokuapp.com/states.json');


}]) // end of MainCtrl

Comments

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.