I'm trying to implement a cross-domain setup for my backbone app.
my server (express.js) is allowing cross domains and credential:
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);
....
});
my client (backbone.js) is configured to accept cross domain as well :
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
};
});
}
});
});
Now when I test my code (let say a POST
request), i have a very special behavior:
var contacts = new Contacts;
contacts.create({'name': 'my name'});
The browser return this message:
OPTIONS ... 404 (Not Found) jquery.js:8419
This totally confuse me as the OPTIONS
http method is not supported by backbone?