I have application using MVC, angular.js , entity framework and Webapi. My Webapi is in same solution but in different project so it has its own port. In my angular service i tried to call webapi like
app.service('MyService', function coursesService($http) {
var self = this;
self.addData = function (data, config) {
return $http.post('http://localhost:1234/api/Test', JSON.stringify(data), config)
.success(function (data, status, headers, config) {
return data;
})
.error(function (data, status, header, config) {
return data;
});
}
});
But when application ran it was throwing error cross origin request. So in my webapi's webapiconfig.cs i had to write below code in Register method
var cors = new EnableCorsAttribute("*", "*", "*");
config.EnableCors(cors);
Then i got rid of this error. But what i would like to know if this is common issue and what is best way to avoid it?