我正在尝试为我的主干应用程序实现跨域设置。
我的服务器(express.js)允许跨域和凭据:
var allowCrossDomain = function(req, res, next) {
var allowedHost = [
'http://localhost:3001',
'http://localhost:7357'
];
if(allowedHost.indexOf(req.headers.origin) !== -1) {
res.header('Access-Control-Allow-Credentials', true);
res.header('Access-Control-Allow-Origin', req.headers.origin)
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version');
next();
} else {
res.send({auth: false});
}
}
app.configure(function(){
....
app.use(allowCrossDomain);
....
});
我的客户端(backbone.js)也配置为接受跨域:
define(["backbone", "jquery", "underscore"], function (BB, $, _) {
return BB.Model.extend({
idAttribute: "_id",
initialize: function () {
var that = this;
$.ajaxPrefilter( function( options, originalOptions, jqXHR ) {
options.crossDomain ={
crossDomain: true
};
options.xhrFields = {
withCredentials: true
};
});
}
});
});
现在,当我测试我的代码(假设是一个POST
请求)时,我有一个非常特殊的行为:
var contacts = new Contacts;
contacts.create({'name': 'my name'});
浏览器返回此消息:
选项 ... 404(未找到)jquery.js:8419
这完全让我感到困惑,因为OPTIONS
骨干网不支持 http 方法?