-1

Two important points at the beginning:

1) I have no access to server side, so I can't configure CORS normally.

2) I can see the response with correct data in Network tab at Chrome Dev Console.

The problem: I receive a very common error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://someorigin' is therefore not allowed access.

So my code goes to error section and the data is null:

getItems: function (count, offset, properties) {
            var deferred = $q.defer();
            var urlBase = 'items/list/';
            var timestamp = getTimestamp();
            var params = {count: count, offset: offset, includedProperties: properties, returnProperties: "true"};
            var query = getQuery(urlBase, params, timestamp);

            $http({
                method: 'GET',
                url: apiLink + query
            }).
            success(function (data, status, headers, config) {debugger;
                deferred.resolve(data);
            }).
            error(function (data, status, headers, config) {debugger;
                deferred.reject(status);
            });
            return deferred.promise;
        }

But I can see the response data in the Network tab.

Is there a way to resolve $http result with received data?

I have to add, that I can't use params object inside $http, because I have to create a HMAC signature on url-query, so the order of query params is important, but using params object places params in url in unpredictable order.

splash27
  • 2,057
  • 6
  • 26
  • 49
  • *"Is there a way to resolve $http result with received data?"* Yes, correct the CORS error. This will require changes at the API. – Kevin B Feb 09 '18 at 16:26
  • @KevinB, I have no access to server side. I can't change the API. Please, read the beginning of my question. – splash27 Feb 09 '18 at 16:29
  • That is unfortunate. Your problem is unsolvable without another server. – Kevin B Feb 09 '18 at 16:29
  • @KevinB, but I can see the responce with correct data in Network tab at Chrome console. So, the browser actually receives the data, but AngularJS prevents `success` resolving. Are you sure that it is unsolvable? – splash27 Feb 09 '18 at 16:37
  • Yes, absolutely. For security reasons, the browser will not let javascript html or css access that. You can get around this problem by having a second server perform the request for you. – Kevin B Feb 09 '18 at 16:38
  • https://stackoverflow.com/questions/29547003/angularjs-no-access-control-allow-origin-header-is-present-on-the-requested-r – Praveen Soni Feb 09 '18 at 17:38
  • hey @splash27, this [blog post](https://daveceddia.com/access-control-allow-origin-cors-errors-in-angular/) will be helpful – Praveen Soni Feb 09 '18 at 17:41

1 Answers1

0

You say that you can see the response in the tools, so you haven't a problem with the CORS. Probably you have a problem with the code. For example you may check what follows:

$http success/error methods are deprecated and have been removed since AngularJS 1.6.0.

You should use promise methods:

$http( ... )
   .then(
       function onSuccess(response){ ... },
       function onError(error){ ... }
    );

Other details here

7uc4
  • 194
  • 1
  • 8