Skip to content

Instantly share code, notes, and snippets.

@a2ikm
Created January 31, 2011 12:12
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 a2ikm/803951 to your computer and use it in GitHub Desktop.
Save a2ikm/803951 to your computer and use it in GitHub Desktop.
Extend jQuery with functions for PUT and DELETE requests.
/*
* Extend jQuery with functions for PUT and DELETE requests.
* references:
* http://homework.nwsnet.de/news/9132_put-and-delete-with-jquery
* http://stackoverflow.com/questions/4007605/using-http-put-to-send-json-with-jquery-and-rails-3
*/
(function($) {
function _ajax_request(url, data, callback, type, method) {
if ($.isFunction(data)) {
callback = data;
data = {};
}
return $.ajax({
type: method,
url: url,
data: data,
success: callback,
dataType: type,
beforeSend: function(xhr){
xhr.setRequestHeader("X-Http-Method-Override", method.toLowerCase());
}
});
}
$.extend({
"put": function(url, data, callback, type) {
return _ajax_request(url, data, callback, type, 'PUT');
},
"delete": function(url, data, callback, type) {
return _ajax_request(url, data, callback, type, 'DELETE');
}
})
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment