Created
June 22, 2018 07:01
-
-
Save hayatbiralem/323418d3d0acda141496fe7cee83ddde to your computer and use it in GitHub Desktop.
Remove Default jQuery Ajax Header
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
$.ajax( | |
'https://api.github.com/search/repositories?q=' + encodeURIComponent(search), | |
{ | |
// '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 = $.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-CSRF-TOKEN') 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; | |
} | |
} | |
) | |
.done( | |
function (res) { | |
console.log(res); | |
vm.clients = res.items; | |
} | |
) | |
.always( | |
function () { | |
loading(false); | |
} | |
) | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment