Created
April 14, 2015 00:18
-
-
Save stevobengtson/35dbb6b5b54bc8db803b to your computer and use it in GitHub Desktop.
jQuery ajax shorthands
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jQuery.each( [ "put", "patch", "delete" ], function( i, method ) { | |
jQuery[ method ] = function( url, data, callback, type ) { | |
// Shift arguments if data argument was omitted | |
if ( jQuery.isFunction( data ) ) { | |
type = type || callback; | |
callback = data; | |
data = undefined; | |
} | |
// The url can be an options object (which then must have .url) | |
return jQuery.ajax( jQuery.extend({ | |
url: url, | |
type: method, | |
dataType: type, | |
data: data, | |
success: callback | |
}, jQuery.isPlainObject( url ) && url ) ); | |
}; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment