通过 HTTPS 的 Ajax GET 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15375908/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax GET request over HTTPS
提问by jviotti
How can I send an ajax GETrequest over HTTPS?
如何通过HTTPS发送 ajax GET请求?
$.getthrows 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 ChromeI'm able to get the response. I see no reason why it shouldn't work work over an ajax request.
如果我使用Chrome导航到 url,我就能得到响应。我看不出为什么它不能在 ajax 请求上工作。
回答by Andrew Moore
You cannot make an AJAX request to an httpspage if you are currently in httpbecause of the Same Origin Policy.
你不能让一个Ajax请求的https,如果你是在当前页面http,因为的同源策略。
The host, portand 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(跨源资源共享)以允许此特定请求。
回答by Dimitar Nikovski
[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):
然而,我发现我不能发送$.ajax()或任何特定的$.get,$.post等因(Chrome的输出):
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. So from the error it looks as if the forward slash wasn't there.
疯狂的是什么修复了它。调用$.get('/api/mycall/')输出上述错误“拒绝连接到‘ http://mywebapp/api/mycall’”,这在代码的实际调用中省略了结尾的正斜杠。因此,从错误看来,好像没有正斜杠。
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.
我已经进行了多次调用,当被调用的 url 中有一个结束斜杠时,每个调用都失败了。同样的人都成功了,没有之一。
So calling $.ajax({ url: '/api/mycall'})works, whilst $.ajax({ url: '/api/mycall/'})doesn't.
所以调用$.ajax({ url: '/api/mycall'})有效,而 $.ajax({ url: '/api/mycall/'})没有。

