6

I try to do ajax, my code written on a https site it request to non https, but the resources were blocked by Chrome.

            $.ajax({
                url : "http://example.com/non-https",
..
..

Previously in earlier version I don't have this issue. Must my ajax request target a https too? Or there is a better way to save that problem?

Amy Johnson
  • 125
  • 1
  • 1
  • 8
  • Almost every current browser nowadays blocks loading of non-HTTPS resources when the main document was loaded via HTTPS. So yes, you have to make your AJAX request to an HTTPS URL too. – CBroe Jan 25 '15 at 07:15
  • 1
    @CBroe means I have to get a cheap ssl for the sake of developing something? – Amy Johnson Jan 25 '15 at 07:17
  • 1
    If you are talking about your local developing – a self-signed certificate will do (once you add an exception in your browser so that it will accept it). Or – don’t use HTTPS to load the main document in the first place … – CBroe Jan 25 '15 at 07:21
  • 1
    @CBroe says I want to inject something (https), my request target must be a https too.. no choice.. – Amy Johnson Jan 25 '15 at 07:22
  • This answer has a pretty good explanation why you should not and will not be able to do it: http://stackoverflow.com/questions/4032104/http-ajax-request-via-https-page – Philip P. Feb 18 '17 at 01:22
  • you can get free SSL certificates easily from https://letsencrypt.org/ so it needn't cost you anything, even in production – ADyson Jul 25 '17 at 15:46
  • 1
    Possible duplicate of [HTTP Ajax Request via HTTPS Page](https://stackoverflow.com/questions/4032104/http-ajax-request-via-https-page) – ADyson Oct 13 '17 at 08:46
  • If your website is `https://mywebsite.com` and you're making AJAX requests via `http://mywebsite.com` then you're definitely going to run into a problem , Just make all your requests HTTPS. – JeanPaul98 Nov 25 '17 at 20:24

1 Answers1

1

Just change the http:// in your url variable to //, so when the page is loaded with http, the ajax request would use the http protocol, if loaded with https, the ajax request protocol would be set by the browser to https. i.e, your code should look like:

           $.ajax({
                url : "//example.com/non-https",
..
..
PalDev
  • 566
  • 8
  • 12