0

I want to retry a call after some condition with Restangular 1.4.0. So I set up a setErrorInterceptor as in the documentation. It works but the then clause of the first call is not executed. The responseHandler returns undefined in the error interceptor.

My client code:

Restangular.setErrorInterceptor(function (response, deferred, responseHandler) {
    console.log('error interceptor');
    console.log(response.status);
    if (response.status === 403) {
        //refreshAccesstoken().then(function() {
        //  // Repeat the request and then call the handlers the usual way.
        //  $http(response.config).then(responseHandler, deferred.reject);
        //  // Be aware that no request interceptors are called this way.
        //});

        return false; // error handled
    } else if (response.status === 401) {
        return $http.get('http://192.168.0.27:3000/auth/guest').then(function (result) {
            var token = result.data.token;
            Auth.setToken(token);
            response.config.headers.Authorization = 'Bearer ' + Auth.getToken();
            console.log(responseHandler);
            //response.config.headers["Authorization"] =  Auth.getToken();
            $http(response.config).then(responseHandler, deferred.reject);
            //return token;
            return false;
        });
        //$http(response.config).then(responseHandler, deferred.reject);

    }
    console.log('error not handled');
    return true; // error not handled
});

Dashboards.post()
    .then(function (result) {
        console.log(result);
        return Restangular.oneUrl('newDash', result.data).get();
    }, function (response) {
        console.log("Error with status code", response.status);
    })
    .then(function (result) {
        console.log(result);
    });

My server log:

POST /api/v1/dashboards HTTP/1.1" 401
GET /auth/guest HTTP/1.1" 200
POST /api/v1/dashboards HTTP/1.1" 201
Sydney
  • 11,964
  • 19
  • 90
  • 142

2 Answers2

0

Instead of responseHandler I use this: deferred.resolve as the first parameter: $http(response.config).then(deferred.resolve, deferred.reject);

Felix
  • 3,999
  • 3
  • 42
  • 66
-1

If you're accessing a REST API from a different domain/host, then you are having CORS issues.

JavaScript (which includes AngularJS) does not allow making requests outside its own domain, unless the server has explicitly allowed it, and your script is specifically asking for access.

This StackOverflow question actually answered my problem, and it now connects to my REST service:

Restangular crossdomain request. What I do wrong?

Community
  • 1
  • 1