编辑,更新@底部
两个域:
Rails-api:http://localhost:3000/
/config/application.rb contains
config.action_dispatch.default_headers = {
'Access-Control-Allow-Origin' => '*',
'Access-Control-Request-Method' => '*'
}
BackboneJS:http : //local.team.com/main.js 包含:
require([
'app/App',
'backbone',
'app/Router',
'bootstrap'
],
function (app, Backbone, Router) {
"use strict";
Backbone.Model.prototype.idAttribute = "_id";
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.url = "http://localhost:3000/" + options.url;
console.log('url = ', options.url)
});
window.MyApp = app;
MyApp.start();
new Router();
Backbone.history.start();
});
当发送主干模型获取时:
url = http://localhost:3000/users main.js:45
OPTIONS http://localhost:3000/users 404 (Not Found) jquery-1.9.0.js:8489
OPTIONS http://localhost:3000/users Origin http://local.team.com is not allowed by Access-Control-Allow-Origin. jquery-1.9.0.js:8489
XMLHttpRequest cannot load http://localhost:3000/users. Origin http://local.team.com is not allowed by Access-Control-Allow-Origin. local.team.com/:1
在这一点上,我完全混淆了哪一端是错误的,我会假设 Backbone。非常感谢任何帮助。
* 更新 *
我现在正在使用 Rails-CORS gem。它适用于获取请求。在我的 config/application.rb 我有:
# https://github.com/cyu/rack-cors
config.middleware.use Rack::Cors do
allow do
origins '*'
resource '*', :headers => :any, :methods => [:get, :post, :put, :options]
end
end
GET 请求,返回 100% 没问题,发布请求导致:
XMLHttpRequest 无法加载 localhost:3000/users。Access-Control-Allow-Origin 不允许来源http://local.team.com 。