0down votefavorite 
<http://stackoverflow.com/questions/29695760/angular-http-dont-receive-response-headers-from-rails-json-api?noredirect=1#>

I do HTTP request using $http from my Angular Yeoman app to JSON API 
service based on Ruby on Rails and set.

I have a controller

angular.module('myApp')
  .controller('ApiwrapperCtrl', [ '$scope', '$http', '$log', '$rootScope', 
'PubNub', '$cookies','$cookieStore', '$window', function($scope, $http, $log, 
$rootScope, PubNub, $cookies, $cookieStore, $window) {....And method sending 
request to API:
    $scope.useHttp = function() {
      var requestURL = 
"http://localhost:3000/api/v1/auth/[email protected]&password=123123123";;
 
      var requestType = "POST"
      var requestHeaders = {'Content-Type': 'application/json'};

      $http({
        method: requestType,
        url: requestURL,
        headers: requestHeaders
      }).success(function(data, status, headers, config) {
        requestHeaders = new Object();
        requestHeaders['access-token'] = headers('access-token');
        requestHeaders['token-type'] = headers('token-type');
        requestHeaders['client'] = headers('client');
        requestHeaders['expiry'] = headers('expiry');
        requestHeaders['uid'] = headers('uid');

        console.log("::: AUTH HEADERS :::");
        console.log(status);
        console.log("|||");
        console.log(data);
        console.log("|||");
        console.log(requestHeaders);
        console.log("....................");

And I got in browser console:

::: AUTH HEADERS :::
apiwrapper.js:228 200
apiwrapper.js:229 |||
apiwrapper.js:230 Objectdata: Object__proto__: Object
apiwrapper.js:231 |||
apiwrapper.js:232 Objectaccess-token: nullclient: nullexpiry: nulltoken-type: 
nulluid: null__proto__: Object
apiwrapper.js:233 ....................

What am I doing wrong? How can I read headers in my Angular app properly?

P.S> Previously it was working and read headers this way successful when 
Angular was a part of Rails project.

Now I use rack-cors on the API serverside. Added to config/application.rb: 

> config.middleware.use Rack::Cors do 
>   allow do
>     origins '*' resource '*', :headers => :any, :methods => [:get, :post, 
> :put, :delete] 
>  end 
> end

to allow requests from external apps.

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to