Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax doesn't send proper ajax headers when using HTTPS

Whenever I create an jQuery.ajax request it works fine when the URL uses the HTTP protocol. But when I send the same request to the HTTPs server, it is sent without the header [HTTP_X_REQUESTED_WITH: XMLHttpRequest]. Thus the server has no way of knowing that this is an AJAX request!

I've tried:

  • Switching $.ajax, $.post, $.get
  • Forcing the header using beforeSend
  • Setting CrossDomain:true

Note: There are no cross-domain issues, the request is valid and handled, but not as AJAX.

This issue happens when the current URL is http but the requested URL is on the same domain but uses HTTPS. http://example.com/home will use AJAX POST to post to

like image 738
mjalajel Avatar asked Mar 14 '12 13:03

mjalajel


3 Answers

I tried Kishor's approach, but the header didn't get sent. I modified it, setting the header like this:

$.ajax {
  beforeSend: function(jqXHR, settings) {
    jqXHR.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
  },
  ...
};

I'm using jQuery 1.8.2.

like image 174
Graham Ashton Avatar answered Oct 23 '22 18:10

Graham Ashton


I got the same issue for doing same stuff between http and https.
I have researched this and it is cross domain issue. Please try JSONP with call back function for doing the stuff, and the most important thing the server side page you are using for doing curl has to set some headers for allowing http to https connection. These are below:

header("Access-Control-Allow-Origin: your https url");
header("Access-Control-Allow-Methods: POST, GET");
header("Access-Control-Max-Age: 1728000");

header("Access-Control-Allow-Headers: Content-Type, Connection, Depth, User-Agent, X-File-Size, X-Requested-With, If-Modified-Since, X-File-Name, Cache-Control");
header("Connection: close");      
like image 27
THE ONLY ONE Avatar answered Oct 23 '22 18:10

THE ONLY ONE


$.ajax {
    ...
    ...
    beforeSend : function(jqXHR, settings) {
                   jqXHR.setRequestHeader("HTTP_X_REQUESTED_WITH", "XmlHttpRequest"
                 }

    ...
    ...
}

Hope this one helps :D

like image 22
Kishor Kundan Avatar answered Oct 23 '22 17:10

Kishor Kundan