当我通过ajax加载https页面时,MixedContent,但浏览器仍然认为它是http

时间:2015-01-28 16:35:34

标签: javascript angularjs https restangular

在网页上安装SSL证书后,我遇到了使用https服务的页面需要带有ajax的http端点的问题。 我正在使用restangular,我更改了基本网址以使用https。

    var uri = location.protocol + "//" + location.host;
    RestangularProvider.setBaseUrl(uri);

有趣的是,当我在chrome开发人员工具中看到请求时,我看到了

Request URL:https://theaddress.com/api/endpoint
Request Headers
Provisional headers are shown
Accept:application/json, text/plain, */*
Referer:https://theadress.com/somepage
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.93 Safari/537.36
X-Requested-With:XMLHttpRequest

所以请求应该是https,但我仍然得到:

Mixed Content: The page at 'https://theaddress.com/somepage' was loaded over HTTPS, but requested an insecure XMLHttpRequest endpoint 'http://theadress.com/api/endpoint'. This request has been blocked; the content must be served over HTTPS.

另外我应该提一下,这发生在prod服务器上,但是在我的本地测试之后它运行正常(我有自签名的ssl证书)之后我使用了包含https的基本网址。

可能是什么问题?

2 个答案:

答案 0 :(得分:9)

我花了4个小时试图解决类似的问题。这就解决了我的问题:

摘要:在您的请求中添加一个尾随'/'

我发现this post对解决我的问题非常有用。基本上,服务器并不关心您是否使用尾随'/'发送请求,因为如果您不添加它,它会在内部路由到'/'。但是,如果路由在内部发生(例如,nginx将请求传递给本地进程),您将获得一个http重定向,这将使您的请求失败。

答案 1 :(得分:0)

我尝试了@ Kadi添加斜杠的解决方案,但它有效,但对我来说更优雅的解决方案是将请求从GET更改为POST,这也解决了问题。

仍然不确定根本原因是什么。