Skip to content

Instantly share code, notes, and snippets.

@bschwartz
Created January 22, 2010 19:44
Show Gist options
  • Save bschwartz/284072 to your computer and use it in GitHub Desktop.
Save bschwartz/284072 to your computer and use it in GitHub Desktop.
// This is a jQuery monkey-patch that changes all PUT and DELETE requests
// into POST requests and sets a "_method" param to the original request method.
//
// This is a workaround for the fact that some corporate networks and
// environments don't allow PUT and DELETE requests.
//
// This whole thing works because Rails implements the _method hack.
(function(){
var ajaxWithoutMethodHack = jQuery.ajax;
jQuery.ajax = function( s ) {
if (s.type == "PUT" || s.type == "DELETE") {
if ( s.data && typeof s.data === "object" ) {
s.data._method = s.type;
} else if (s.data && typeof s.data === "string" ) {
s.data = s.data + '&_method=' + s.type;
} else {
s.data = {'_method': s.type};
}
s.type = 'POST';
}
ajaxWithoutMethodHack(s);
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment