0

Hey guys I am trying to use an EPA API that provides daily UV Index information in JSON.

The link I am trying to read at the moment is:

http://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/33126/JSON?callback=callBackFn

If you open that link it shows valid JSON, but when I use it in my Angular.js code it does not read it, and my variable stays as unknown. My code is:

var tanApp = angular.module('tanApp')
.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
$scope.awesomeThings = [
  'HTML5 Boilerplate',
  'AngularJS',
  'Karma'
];
$scope.data = 'unknown';
    $http.get('http://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/33126/JSON?callback=callBackFn').success(function(data){
        $scope.data = data;
});
    tanApp.config(['$httpProvider', function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }]);

My HTML code is {{data}}.

When I take out the "JSON" portion, it comes up as XML, since it is the default, and that is actually showing up, but I need it as JSON.

Can someone possibly get this to work or provide some help? I can offer bitcoin as a bounty.

Thanks!

2 Answers 2

1

The problem is that the URI you're using returns raw JSON instead of JSONP. You can either setup a server-side proxy or check with your API provider to see if there's a way to get a valid JSONP response.

If you can get valid JSONP, you will also have to setup a callback to handle the response.

In that case, reference the accepted answer for this question.

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

5 Comments

So there is nothing I can do with the raw JSON? The API is a RESTful Data Service API, does that change anything?
I don't see any way to get a JSONP response from their servers, and their JSON response is not returned with the necessary headers for CORS. So, it appears that your only option is to setup a proxy, either on the same domain your app is hosted on or another server that will provide you with the necessary CORS headers or valid JSONP.
I really appreciate it marck, can you point me to a resource where I can learn more about setting up this proxy?
Check out this answer including the caveat. You may want to contact the service provider and see if they can suggest the best approach that would fit their guidelines.
Thanks Mark. I will likely contact them and see what they say.
0

Have you tried this using $http.jsonp() instead of $http.get(). Also if your using $http.jsonp() then the callback should be set to "JSON_CALLBACK".

e.g.

$http.jsonp('http://iaspub.epa.gov/enviro/efservice/getEnvirofactsUVHOURLY/ZIP/33126/JSON?callback=JSON_CALLBACK')
    .success(function(data){
        $scope.data = data;
    }

1 Comment

Still getting unknown. Not sure if maybe I have to sign up for API access?

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.