gpt4 book ai didi

ruby-on-rails - Rails 3 和 Angularjs Origin http ://localhost is not allowed by Access-Control-Allow-Origin.

转载 作者:行者123 更新时间:2023-12-04 04:35:54 25 4
gpt4 key购买 nike

首先,让我说我已经尝试了在这里发布的大量解决方案。他们似乎都不适合我。我意识到我的问题源于 CORS,而我的 Rails 应用程序无法正确处理。但是,我不太确定如何纠正这个问题。我希望这里有人可以对这个问题有所了解。

相关代码片段:

Rails application_controler.rb

class ApplicationController < ActionController::Base
protect_from_forgery
#before_filter :cors_preflight_check
before_filter :allow_cross_domain_access
after_filter :cors_set_access_control_headers

# For all responses in this controller, return the CORS access control headers.

def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Max-Age'] = "1728000"
end

def cors_preflight_check
if request.method == :options
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = 'X-Requested-With, X-Prototype-Version'
headers['Access-Control-Max-Age'] = '1728000'
render :text => '', :content_type => 'text/plain'
end
end

def allow_cross_domain_access
headers['Access-Control-Allow-Origin'] = '*'# http://localhost:9000
headers['Access-Control-Allow-Headers'] = 'GET, POST, PUT, DELETE, OPTIONS'
headers['Access-Control-Allow-Methods'] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
headers['Access-Control-Max-Age'] = '1728000'
end
end

Angular App.js
]).config(function($httpProvider){
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
function loginCtrl ($scope,$http) {
$scope.master = {};

$scope.isUnchanged = function(user) {
return angular.equals(user, $scope.master);
};

$scope.login = function (user) {
$http({
method: 'POST',
/*url: 'http://localhost/test/quote.php',*/
url: 'http://localhost:3000/api/sessions/',
data: user
}).success(function(data)
{
alert("call phonegap store data function:" + data.access_token)
}).error(function (data, status, headers, config) {
alert("error" )
});
};

}

来自 chorme 的错误信息
OPTIONS http://localhost:3000/api/sessions/ 404 (Not Found) angular.js:6730
OPTIONS http://localhost:3000/api/sessions/ Origin http://localhost is not allowed by Access-Control-Allow-Origin. angular.js:6730
XMLHttpRequest cannot load http://localhost:3000/api/sessions/. Origin http://localhost is not allowed by Access-Control-Allow-Origin.

来自 rails 服务器的错误消息
Started OPTIONS "/api/sessions/" for 127.0.0.1 at 2013-11-03 22:30:54 -0500

ActionController::RoutingError (uninitialized constant SessionsController):
activesupport (3.2.13) lib/active_support/inflector/methods.rb:230:in
`block in constantize'
activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `each'
activesupport (3.2.13) lib/active_support/inflector/methods.rb:229:in `constantize'
actionpack (3.2.13) lib/action_dispatch/routing/route_set.rb:69:in `controller
_reference' .....


....C:/Ruby/lib/ruby/2.0.0/webrick/httpserver.rb:138:in `service'
C:/Ruby/lib/ruby/2.0.0/webrick/httpserver.rb:94:in `run'
C:/Ruby/lib/ruby/2.0.0/webrick/server.rb:295:in `block in start_thread'


Rendered C:/Ruby/lib/ruby/gems/2.0.0/gems/actionpack-
3.2.13/lib/action_dispatch/middleware/templates/rescues/routing_error.erb within
rescues/layout (0.0ms)

对此的任何帮助将不胜感激!

最佳答案

我想通了。

    after_filter :cors_set_access_control_headers

# For all responses in this controller, return the CORS access control headers.

def cors_set_access_control_headers
headers['Access-Control-Allow-Origin'] = '*'
headers['Access-Control-Allow-Methods'] = 'POST, GET, OPTIONS'
headers['Access-Control-Allow-Headers'] = %w{Origin Accept Content-Type X-Requested-With X-CSRF-Token}.join(',')
headers['Access-Control-Max-Age'] = "1728000"
end

我将上面的代码放在我所有的 api Controller 都继承自的基本 Controller 中。问题是复制和粘贴解决方案。 ALLOW-headers 和 Allow-Methods 值被反转。那修复了它。

关于ruby-on-rails - Rails 3 和 Angularjs Origin http ://localhost is not allowed by Access-Control-Allow-Origin.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19761437/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com