使用带有路径URL的SSL请求(json)通过SSL会出现混合内容错误?

bbe*_*ort 5 javascript ssl json d3.js

我不确定这里发生了什么 - 但我想我会问群众.基本上我有一个获取JSON数据函数与D3在我的本地开发服务器上工作,但当我将其移动到生产(通过SSL托管)时,我收到以下错误:

Mixed Content: The page at 'https://myapp.com/' was loaded over HTTPS, 
but requested an insecure XMLHttpRequest endpoint 'http://myapp.com/path/to/mydata/'. 
This request has been blocked; the content must be served over HTTPS.
Run Code Online (Sandbox Code Playgroud)

它比纯HTTP工作得好.问题是,我没有指定方案甚至是端点,我只是使用来自Web服务器根目录的绝对路径,如下所示:

var url = '/path/to/mydata/'
d3.json(url, function(error, data) {
    // do something with data
}); 
Run Code Online (Sandbox Code Playgroud)

无论如何我可以强迫这个使用SSL吗?类似于"//cdn.com/path/to/asset.js"省略方案的地方,允许根据服务器的内容进行SSL和普通HTTP请求.

更新

更多信息:我在Heroku上托管这个,对我的API的其他请求似乎工作得很好,包括那些使用d3.csvjQuery.get.

Tim*_*ann 0

不知道为什么 D3 在这种情况下坚持使用纯 http。作为解决方法,您可以简单地检测协议并相应地调整 url:

var url = window.location.protocol + '//path/to/mydata/'
d3.json(url, function(error, data) {
    // do something with data
}); 
Run Code Online (Sandbox Code Playgroud)