Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Last active August 29, 2015 14:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/6836bdb13f00048f920b to your computer and use it in GitHub Desktop.
Save barneycarroll/6836bdb13f00048f920b to your computer and use it in GitHub Desktop.
Avoid preflight OPTIONS conflicts when attempting to POST with a CORS XHR in Mithril. This only solves the use case of simple POST attempts with web clients other than Safari (which sets custom headers on CORS POST requests): it will not work for situations in which any custom headers are added to the request – in these cases an OPTIONS prefligh…
m.cors = 'FormData' in window ? function( request ){
var input = request.data;
if( input ){
delete request.data;
}
else {
return m.request( request );
}
request.serialize = function(){
var formData = new FormData();
for( var key in input ){
formData.append( key, input[ key ] );
}
return formData;
}
return m.request( request );
} : m.request;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment