Skip to content

Instantly share code, notes, and snippets.

@7kfpun
Created June 25, 2015 10:09
Show Gist options
  • Save 7kfpun/a8d1326db44aa7857660 to your computer and use it in GitHub Desktop.
Save 7kfpun/a8d1326db44aa7857660 to your computer and use it in GitHub Desktop.
Remove the X-Requested-With header from ajax requests
http://stackoverflow.com/questions/3372962/can-i-remove-the-x-requested-with-header-from-ajax-requests
jQuery.ajax({
url: yourAjaxUrl,
// 'xhr' option overrides jQuery's default
// factory for the XMLHttpRequest object.
// Use either in global settings or individual call as shown here.
xhr: function() {
// Get new xhr object using default factory
var xhr = jQuery.ajaxSettings.xhr();
// Copy the browser's native setRequestHeader method
var setRequestHeader = xhr.setRequestHeader;
// Replace with a wrapper
xhr.setRequestHeader = function(name, value) {
// Ignore the X-Requested-With header
if (name == 'X-Requested-With') return;
// Otherwise call the native setRequestHeader method
// Note: setRequestHeader requires its 'this' to be the xhr object,
// which is what 'this' is here when executed.
setRequestHeader.call(this, name, value);
}
// pass it on to jQuery
return xhr;
},
success: function(data, textStatus, jqXHR) {
// response from request without X-Requested-With header!
}
// etc...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment