你能否告诉我我哪里出错了,是否有任何遗漏我必须添加以从heroku网站获取json
config.ru
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
use Rack::Cors do
allow do
origins 'localhost:3000', '127.0.0.1:3000', 'http://stark-anchorage-86169.herokuapp.com/ages/ getfortest', /\Ahttp:\/\/192\.168\.0\.\d{1,3}(:\d+)?\z/
resource '/file/list_all/', :headers => 'x-domain-token'
resource '/file/at/*',
:methods => [:get, :post, :delete, :put, :patch, :options, :head],
:headers => 'x-domain-token',
:expose => ['Some-Custom-Response-Header'],
:max_age => 600
end
allow do
origins '*'
resource '/public/*', :headers => :any, :methods => :get
end
end
配置/ application.rb中
Bundler.require(*Rails.groups)
module AppV11
class Application < Rails::Applications.
config.active_record.raise_in_transactional_callbacks = true
config.middleware.insert_before 0, "Rack::Cors" do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :options]
end
end
end
end
我的控制器看起来像下面的一些助手
before_filter :set_access_control_headers
def set_access_control_headers
headers['Access-Control-Allow-Origin'] = 'http://staranchorage- 86169.herokuapp.com/ages/getfortest'
end
Angularjs部分:
function getpost()
{
console.log("INSIDE GETPOST");
$http.get('http://stark-anchorage-86169.herokuapp.com/ages/getfortest')
.success(function(response)
{
console.log("INSIDE RESPONSE "+ response);
$scope.demogetpost = response.data;
console.log( $scope.demogetpost);
console.log("NOT PRINTING");
})
return $scope.demogetpost;
}
答案 0 :(得分:0)
我一直在新标签中打开,这就是为什么我阻止了Cross-Origin请求的原因。我还为Access-Control-Allow-Origin添加了标题。