3

I'm facing an issue when calling API, let me explain architecture first.

Whole font-end application os AngularJS 1.5 based, hosted on domains like below: app1.mydomain.com, app2.mydomain.com, appN.mydomain.com

I have an API (Symfony2 based - FOS Rest) which is on:

api.mydomain.com

Server is Nginx, php5 (fpm), CORS open.

Now, lets describe problem:

Im calling an API endpoint for some data:

    var data = {someProperty: someValue};
    var $p = $resource($rootScope.apiDomain + '/some/endpoint', {}, {
        get: {
            method: 'GET'
        }
    }).get(data).$promise;
    var request = $q.all({request: $p});

    return request
        .then(function (response) {
            // code on success
        }, function (error) {
            // code on error
        });

Now let's say that server's response is ANY of 400's or 500's, then in ResponseError (interceptor) i've should get this code as an 'status' value

responseError: function (response) {
    if (403 === response.status) {  
        // something on 403
    }
    // do something on other codes
    return $q.reject(response);
}

No matter which response code is sent back from backend server, the response object contains following:

Object {data: null, status: -1, config: Object, statusText: ""}

However... i've seen THIS ANSER and this is basically what i do have now...

So:

  1. Is there any way to solve such issue (i have, let's say full possibility in configuration of both back&front in any way)
  2. If it's not "solvable" easily, then how to?
Cœur
  • 37,241
  • 25
  • 195
  • 267
kptLucek
  • 31
  • 7
  • This looks like a CORS problem. What does the console say? What does the network panel say about the response? – JB Nizet Aug 20 '16 at 14:05
  • Yeah itll be CORS. Ensure you setup CORS on front and backend, theres load of info if you google but to get you started heres a similar question http://stackoverflow.com/questions/21455045/angularjs-http-cors-and-http-authentication – Chris Aug 20 '16 at 14:15
  • Thanks for replies, basicly as i said, i do have CORS open as follows: [pastebin](http://pastebin.com/aMHye04D) @JBNizet console says correct ResponseCode (ie. 403) – kptLucek Aug 20 '16 at 14:32

1 Answers1

0

Well...

I've finally managed to make this working - nginx.opencors config

Solution:

(before)

add_header 'Access-Control-Allow-Origin' "*";

ResponseObject: {data: Object, status: -1, config: Object, statusText: ""}

(after)

add_header 'Access-Control-Allow-Origin' "*" always;

ResponseObject: {data: Object, status: 403, config: Object, statusText: "Forbidden"}

kptLucek
  • 31
  • 7