레일 API를 사용하고 각도를 사용하여 프론트 엔드에 전원을 공급하기 위해 rails-api 보석을 사용하고 있습니다. $ http를 사용할 때마다 data
대신 params
을 전달하면 작동합니다. 다음은 사용자가 로그인하고 새 세션을 만들려고과 예입니다 :
'use strict';
app.controller('LoginCtrl', function($scope, $location, $http, tokenHandler) {
$scope.login = function() {
$http({
url: 'http://localhost:3000/api/admins/sign_in',
method: 'POST',
params: $scope.admin
}).success(function(data) {
if (data.success) {
$scope.ngModel = data.data.data;
tokenHandler.set(data.data.auth_token);
$location.path('/admin/blog');
} else {
$scope.ngModel = data;
$scope.user.errors = data.info;
}
}).error(function(msg) {
$scope.admin.errors = 'Something is wrong. Please try again.';
});
};
});
을하는 경우 대신 PARAMS의 내가 data: { admin: $scope.admin }
을 사용, 레일은 params[:admin]
이 전무는 것을 나에게 불평. 전혀 오지 않는 것 같습니다.
Started POST "/api/admins/[email protected]&password=[FILTERED]" for 127.0.0.1 at 2014-09-07 20:08:04 -0400
Processing by Admin::SessionsController#create as HTML
Parameters: {"email"=>"[email protected]", "password"=>"[FILTERED]"}
어떤 내가 작업 할 수 있습니다 : 나는 PARAMS를 사용하는 경우
그러나,이 얻을. 요청이 HTML로 처리 될 때에 만 작동하는 것처럼 보이는 것은 이상한 일입니다. 내가data
를 사용하는 경우,이 얻을 :
Started OPTIONS "/api/admins/sign_in" for 127.0.0.1 at 2014-09-07 20:36:24 -0400
Processing by Admin::SessionsController#create as */*
그것은
*/*
에 의해 처리를 말할 생각인가? 나는 그것이 구체적으로 json에 의해 처리되기로되어 있다는 것을 이해해야한다고 생각한다.
내 세션 컨트롤러는 다음과 같습니다
class Admin::SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token
before_filter :authenticate_user!, except: [:create]
respond_to :json
# ...
end
이상한 것은 내가 확실히 단지 data: { admin: $scope.admin }
를 사용하여 첫 번째 작업 시간을 가지고,하지만 그 이후의 PARAMS 내가 params: $scope.admin
를 사용하지 않으면 나오지 않을 것 같다. ALSO
:
I 인증을 위해 고안를 사용하고, 나는 내와 ApplicationController이를 추가했다 : 지금이 전에 처리
class ApplicationController < ActionController::API
include ActionController::MimeResponds
before_filter :set_cors_headers
before_filter :cors_preflight
private
def set_cors_headers
headers['Access-Control-Allow-Origin'] = AppConfig.client['origin']
headers['Access-Control-Allow-Methods'] = 'GET,POST,PUT,DELETE,OPTIONS'
headers['Access-Control-Allow-Headers'] = '*'
headers['Access-Control-Max-Age'] = "3628800"
end
def cors_preflight
head(:ok) if request.method == :options
end
end
누구를?