CORS with angular.js $ and ServiceStack

I have a REST Service Service Service and an angular.js client. The network is hosted on a different port, then in the RESTS service.

Retrieving data using $ http does not work, but using $ resource does not work.

in the REST service, I set the following global headers:

GlobalResponseHeaders = { { "Access-Control-Allow-Origin", "*" }, { "Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS" }, { "Access-Control-Allow-Headers", "X-Requested-With"} } 

Now in angular.js I can successfully read methods using $ http, for example

 $http({ method:'get', url:address, requesttype:'application/json' }).success(function(data) { $scope.ServiceStatus = data; }); 

but reading the same address with $ ressource my code for factory does not work:

 myApp.factory('qaServiceStatus', function($resource){ return $resource('http://localhost:1338/api/status', {}, { query: {method:'GET', headers: {'Content-Type': 'application/json'}, params:{}} }); }); 

and I use it that way in the controller

 $scope.RESTStatus = qaServiceStatus.get({}); 

error I get: Origin http://localhost:7000 not allowed by Access-Control-Allow-Origin.
and also XMLHttpRequest cannot load http://localhost/api/status . Origin http://localhost:7000 not allowed by Access-Control-Allow-Origin.

what am I doing wrong?

I also tried changing the allowed source to the exact address, but this does not help

+4
source share
1 answer

This is an API that responds to your client and does not allow your client to create a CORS request. I don’t know which backend you are using, but you need to look at Access-Control-Allow-Origin settings to make it work.

Update:

Error or function, you need to ask the angularjs command. Atm they interpret: port as a variable, not a port. The solution is to avoid:

Example:

 $resource('http://localhost:1338\:1338/api/status', {}, {..... 
+8
source

Source: https://habr.com/ru/post/1501795/


All Articles