How do I remove x controlled with from an ajax request in jQuery

  • 2020-03-30 00:48:53
  • OfStack

X-Requested-With is often used to determine whether an ajax request is made

However, there were times when we needed to remove the x-supplied-with

Here's one way to do it: js code
 
$.ajax({ 
url: 'http://www.zhangruifeng.com', 
beforeSend: function( xhr ) { 
xhr.setRequestHeader('X-Requested-With', {toString: function(){ return ''; }}); 
}, 
success: function( data ) { 
if (console && console.log){ 
console.log( 'Got data without the X-Requested-With header' ); 
} 
} 
}); 

In addition, the Java code determines the way the ajax request is made
 
if (request.getHeader("x-requested-with") != null 
&& request.getHeader("x-requested-with").equalsIgnoreCase("XMLHttpRequest")) { 
//An asynchronous request
}else{ 

} 

Related articles: