Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an AJAX-request look like a regular, normal request

What parameters on a $.ajax must I set to try and mask the AJAX-request as a normal request? I guess it has to do with the right headers.

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

Is there any way to take out the Origin-header?

At this moment I'm getting different results from the same URL if I surf to it through the web-browser and when I do an jQuery AJAX-request.

like image 300
Seb Nilsson Avatar asked Dec 10 '22 07:12

Seb Nilsson


2 Answers

Due to Same Origin Policy enforced by all modern browsers, this is not possible.

like image 108
Seb Nilsson Avatar answered May 17 '23 06:05

Seb Nilsson


The only thing that differs in an AJAX request sent with jQuery compared to a normal request (whatever you mean by normal request) is the X-Requested-With: XMLHttpRequest HTTP header that is added. This header could be removed like this:

$.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 ''; }
            }
        );
    }
});
like image 21
Darin Dimitrov Avatar answered May 17 '23 08:05

Darin Dimitrov