12

How can I send an ajax GET request over HTTPS?

$.get throws this:

XMLHttpRequest cannot load https://********. Origin null is not allowed by Access-Control-Allow-Origin. 

Is there another way or some workaround to get this working?

If I navigate to the url with Chrome I'm able to get the response. I see no reason why it shouldn't work work over an ajax request.

jviotti
  • 17,881
  • 26
  • 89
  • 148
  • It's looks like a cross-origin issue, not HTTPS. Whatever code you've used, you should provide that with some context on the URL in relation to the page/site it's running in. – Jared Farrish Mar 13 '13 at 02:22
  • Are you calling $.get with an object with your details as in http://api.jquery.com/jQuery.get/ or just by itself? – minikomi Mar 13 '13 at 02:25

2 Answers2

34

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.

You can either make sure that the originating page is on the same host and scheme or implement CORS (cross-origin resource sharing) on the target domain to permit this particular request.

Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
0

[jQuery v. 3.3.1]

I have a web application, where all resources and traffic are via HTTPS.

Yet, I found that I can't send $.ajax() or any of the specific $.get, $.post, etc. due to (Chrome output):

Refused to connect to 'http://mywebapp/api/mycall' because it violates the following Content Security Policy directive: "connect-src 'self'".

It was due to the HTTPS page making the AJAX requests through HTTP and I found no way to force HTTPS.

What is crazy is what fixed it. Calling $.get('/api/mycall/') outputs the above error with "Refused to connect to 'http://mywebapp/api/mycall'", which omits the ending forward slash in the actual call in the code. So from the error it looks as if the forward slash wasn't there.

I have done multiple calls and every single one fails when there is an ending slash in the url being called. The same ones all succeed without one.

So calling $.ajax({ url: '/api/mycall'}) works, whilst $.ajax({ url: '/api/mycall/'}) doesn't.

Dimitar Nikovski
  • 973
  • 13
  • 15