Let's say this piece of code is in a different domain (Server A) and I wanted to work on my local machine (Server B).
HTML Code:
<html ng-app="myApp">
<body ng-controller="empInfoCtrl as employeeList">
<p>Employee Information</p>
<section>
<ul>
<p ng-show="!employeeList.fileError" ng-repeat="employee in employeeList.employees"> {{employee.id}} - {{employee.jobTitleName}}</p>
</ul>
</section>
<p style="color:red" ng-show="employeeList.fileError"> <b>Response:</b> {{employeeList.employees}} </p>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.6.3/angular.min.js"></script>
<script src="app.js"></script>
</body>
</html>
Controller (JavaScript file):
var app = angular.module("myApp", []);
app.controller('empInfoCtrl', function ($http) {
var employeeList = this;
employeeList.fileError = false;
$http.get('employees.json')
.then(function (response) {
employeeList.employees = response.data.empdata;
}, function (response) {
employeeList.fileError = true;
employeeList.employees = response.statusText;
})
});
What if I want to access it through a different domain? I tried running it on my local machine's http-server. But then, I was getting this error:
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access.
How do I modify my Controller in order to be compatible with CORS.