gschervish
gschervish

Reputation: 293

Is it possible to AJAX get the same url in HTTP and HTTPS

I know that you can't do an AJAX request from HTTP to HTTPS so we are working on getting the served content available on both HTTP and HTTPS. Is there away to tell AJAX to get the file based on the protocol its using in the browser?

For example could you do:

....
type: "GET",
url: '//wp-content/themes/twentyeleven/js/jobopenings.json'
....

with the // would it serve up the correct version automatically?

Upvotes: 1

Views: 1172

Answers (3)

jakee
jakee

Reputation: 18566

The window.location object can tell the current used protocol

url: window.location.protocol + url_without_protocol

But the same effect can be reached with relative urls

url: '/path/to/whatever/you/want.json'

This will take the server root and stick your stuff after it, taking the protocol with it.

Hope this helps!

Upvotes: 2

gen_Eric
gen_Eric

Reputation: 227310

Just do /wp-content/themes/twentyeleven/js/jobopenings.json. Note the single slash.

Starting the URL with / tells it took at the root of your domain (which will automatically use the correct http(s) protocol).

Upvotes: 0

Fenton
Fenton

Reputation: 251262

You could use:

var protocol = window.location.protocol || document.location.protocol;

Which you can then use for your url...

....
type: "GET",
url: protocol + '//your-address.com/wp-content/themes/twentyeleven/js/jobopenings.json'
....

Upvotes: 0

Related Questions