Search code examples
angularjssymfonynginxcorsangular-resource

Angular 1.5, response status on error is always -1


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?

Solution

  • 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"}