How to make an AJAX request look like a regular regular request

What parameters for the $.ajax parameter need to be configured to mask the AJAX request as a regular request? I assume this is due to the correct headers.

I think most of the problem is that when working on a local .html jQuery file sets the header value for Origin to null .

Is there a way to extract the Origin header?

At this point, I get different results from the same URL if I view it through a web browser and when I execute a jQuery AJAX request.

+3
source share
3 answers

Due to the same origin policy applied by all modern browsers, this is not possible .

+4
source

The only thing that differs in the AJAX request sent with jQuery compared to the regular request (regardless of the regular request) is the X-Requested-With: XMLHttpRequest HTTP header, which is added. This header can be deleted as follows:

 $.ajax({ url: '/foo', type: 'POST', data: { bar: 'baz' }, beforeSend: function(xhr) { xhr.setRequestHeader( 'X-Requested-With', { toString: function() { return ''; } } ); }, success: function(result) { alert(result); } }); 

or globally, for all AJAX requests on your site:

 $.ajaxSetup({ beforeSend: function(xhr) { xhr.setRequestHeader( 'X-Requested-With', { toString: function() { return ''; } } ); } }); 
+6
source
  • jQuery does not set the Origin header. Only the browser itself can do this. And jQuery (or javascript) does not have authority over this header.

  • here is a link about Origin header cases for null Null Origin Header

  • The only difference between jquery and regular query is really X-Requested-With: XMLHttpRequest, you can remove it manually or you can make queries with the new XMLHttpRequest or with fetch ().

luck

0
source

All Articles