简体   繁体   English

通过HTTPS的Ajax GET请求

[英]Ajax GET request over HTTPS

How can I send an ajax GET request over HTTPS ? 如何通过HTTPS发送ajax GET请求?

$.get throws this: $.get抛出这个:

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. 如果我使用Chrome导航到网址,我就能得到回复。 I see no reason why it shouldn't work work over an ajax request. 我认为没有理由不应该为ajax请求工作。

You cannot make an AJAX request to an https page if you are currently in http because of the Same Origin Policy . 如果您因为同源策略而当前处于http ,则无法向https页面发出AJAX请求。

The host , port and scheme (protocol) must be the same in order for the AJAX request to work. 主机端口方案 (协议)必须相同才能使AJAX请求起作用。

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. 您可以确保原始页面位于同一主机和方案上,也可以在目标域上实现CORS (跨源资源共享)以允许此特定请求。

[jQuery v. 3.3.1] [jQuery v.3.3.1]

I have a web application, where all resources and traffic are via HTTPS. 我有一个Web应用程序,其中所有资源和流量都通过HTTPS。

Yet, I found that I can't send $.ajax() or any of the specific $.get , $.post , etc. due to (Chrome output): 然而,我发现由于(Chrome输出),我无法发送$.ajax()或任何特定的$.get$.post等:

Refused to connect to ' http://mywebapp/api/mycall ' because it violates the following Content Security Policy directive: "connect-src 'self'". 拒绝连接到' http:// mywebapp / api / mycall ',因为它违反了以下内容安全策略指令:“connect-src'self'”。

It was due to the HTTPS page making the AJAX requests through HTTP and I found no way to force HTTPS. 这是由于HTTPS页面通过HTTP发出AJAX请求,我发现无法强制使用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. 调用$.get('/api/mycall/')输出上面的错误,“拒绝连接到' http:// mywebapp / api / mycall '”,这省略了代码中实际调用中的结束正斜杠。 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. 我已经完成了多次调用,并且当调用的url中有一个结束斜杠时,每个调用都会失败。 The same ones all succeed without one. 如果没有一个,那些都是成功的。

So calling $.ajax({ url: '/api/mycall'}) works, whilst $.ajax({ url: '/api/mycall/'}) doesn't. 所以调用$.ajax({ url: '/api/mycall'})有效的,而$.ajax({ url: '/api/mycall/'})却没有。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM