Skip to content

Instantly share code, notes, and snippets.

@jhogervorst
Last active January 1, 2016 15:48
Show Gist options
  • Save jhogervorst/8166217 to your computer and use it in GitHub Desktop.
Save jhogervorst/8166217 to your computer and use it in GitHub Desktop.
Semi-elegant function to change jQuery's default AJAX settings ($.ajaxSetup) within a certain block of code.
/*
* jQuery.ajaxEnvironment.js
* https://gist.github.com/jhogervorst/8166217
*
* Copyright (C) 2013 Jonathan Hogervorst. All rights reserved.
* This code is licensed under MIT license.
*/
(function($) {
$.ajaxEnvironment = function(settings, block) {
var originalSettings = $.ajaxSetup();
var restoredSettings = {};
$.each(settings, function(key) {
restoredSettings[key] = originalSettings[key];
});
$.ajaxSetup(settings);
block();
$.ajaxSetup(restoredSettings);
};
})(jQuery);

jQuery.ajaxEnvironment

Semi-elegant function to change jQuery's default AJAX settings ($.ajaxSetup) within a certain block of code.

Usage

$.ajaxEnvironment({
	contentType: false,
	processData: false,
	// all AJAX settings you want to change
}, function() {
	// do whatever you want to do while the settings are changed
	$.ajax(...);
});

Compatibility

This code has been tested with jQuery 1.9.1 in Safari on Mac.

License

This code is licensed under MIT license.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment