Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax using https on an http page

My site uses http and https protocol; it doesn't affect the content. My site uses jQuery ajax calls, which fills some areas on the page, too.

Now, I would like to do all ajax calls over https. (please dont ask me why :)) When I am on a page with https protocol, ajax requests are working. When I'm on a page with http protocol, I get a javascript error: Access to restricted URI denied

I know that this is a cross domain problem (in fact, it's a cross protocol problem), and I know that I should use the same protocol in ajax calls as on the current page.

Still, I want to all ajax calls to be https, and call them on a page that was served over http. Is there any workaround to achieve this (some json/proxy solution?), or is it simply impossible?

like image 511
user135863 Avatar asked Jul 09 '09 19:07

user135863


People also ask

Does AJAX work with https?

You cannot make an AJAX request to an https page if you are currently in http because of the Same Origin Policy. The host, port and scheme (protocol) must be the same in order for the AJAX request to work.

Can we send request from https to HTTP?

You can't. Browsers prevents this by default as otherwise it would be insecure to allow it. The remote server must have a valid SSL certificate and use HTTPS.

Is AJAX based on HTTP?

With both AJAX and non-AJAX the browser sends HTTP requests and receives HTTP responses from the web server. Deference between an AJAX request and a non-AJAX request is that, AJAX requests: work in the background seamlessly, and. don't make the pages to reload every time a response is received.

Can we use HTTP GET or POST for AJAX calls?

jQuery - AJAX get() and post() Methods. The jQuery get() and post() methods are used to request data from the server with an HTTP GET or POST request.


2 Answers

Add the Access-Control-Allow-Origin header from the server

Access-Control-Allow-Origin: https://www.mysite.com 

http://en.wikipedia.org/wiki/Cross-Origin_Resource_Sharing

like image 149
DalSoft Avatar answered Oct 15 '22 21:10

DalSoft


Try JSONP.

most JS libraries make it just as easy as other AJAX calls, but internally use an iframe to do the query.

if you're not using JSON for your payload, then you'll have to roll your own mechanism around the iframe.

personally, i'd just redirect form the http:// page to the https:// one

like image 25
Javier Avatar answered Oct 15 '22 22:10

Javier