1

Edit: Added new comment with the way i solved my problem.

I have a sample json on my server http://xxx.xxx.x.xx/data.json (i can see it if i directly access it)

[
{"name":"Neptune", "distance":30.087},
{"name":"Uranus", "distance":19.208},
{"name":"Saturn", "distance":9.523}, 
{"name":"Jupiter", "distance":5.203}, 
{"name":"Mars", "distance":1.524}, 
{"name":"Earth", "distance":1.0}, 
{"name":"Venus", "distance":0.723}, 
{"name":"Mercury", "distance":0.387}    
]

An I want to load it on my app (later on I'll bee packaging it with phonegap) - even though I do tests on my browser of course.

And I have this HTML and script:

<!DOCTYPE html>
<html>
<head>
<script   src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.1
5/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="planetController"> 
<ul>
  <li ng-repeat="name in names">
    {{ name.name + ', ' + name.distance }}
  </li>
</ul>
</div>
<script>
var app = angular.module('myApp', []);
app.controller('planetController', function($scope, $http) {
$http.get("http://xxxxxxxx/data.json")
.success(function(response) {$scope.names = response;});    
app.config(['$httpProvider'], function($httpProvider) {
    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
})  
});
</script>

I get the No 'Access-Control-Allow-Origin' header is present on the requested resource. I read most of the topics about it but no help, I still ge tthe error even if I modified the script.

1x2x3x4x
  • 592
  • 8
  • 26
  • 2
    Are you trying to access the json from a domain different than the one the json is on? – MeLight Jul 27 '15 at 09:49
  • 1
    It's not comming from you front-end. The missing CORS is in the back-end. You have to add the authorization in your back-end and handle the `OPTIONS` request – Pierre-Alexandre Moller Jul 27 '15 at 09:49
  • 1
    possible duplicate of ["No 'Access-Control-Allow-Origin' header is present on the requested resource"](http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-resource) – phuzi Jul 27 '15 at 09:49
  • It's a different domain and yes it's IIS. I also have access to the Apache files. – 1x2x3x4x Jul 27 '15 at 10:05

1 Answers1

0

I solved this by adding:

<IfModule mod_headers.c>
       Header set Access-Control-Allow-Origin: *
</IfModule>

in the httpd.conf

And uncommented the LoadModule headers_module modules/mod_headers.so line in the same file.

1x2x3x4x
  • 592
  • 8
  • 26