我正在我的网站上做一些非常基本的 jQuery ajax 操作,但我遇到了很多麻烦。
相关代码如下:
$(document).ready( function() {
$("#getdatabutton").click( function() {
$.ajax({
url: "/jsontest/randomdata",
type: "get",
data: [{name:"ymax", value:$("#randomgraph").height()},
{name:"count", value:$("#countinput").val()},
{name:"t", value:Math.random()}],
success: function(response, textStatus, jqXHR) {
data = JSON.parse(response);
updateGraph(data);
$("#result").html(response);
if(data["error"] == "") {
$("#errorbox").html("None");
}
else {
$("#errorbox").html(data["error"]);
}
},
error: function(jqXHR, textStatus, errorThrown) {
$("#errorbox").html(textStatus + " " + errorThrown);
}
});
});
});
页面是通过 HTTPS 加载的,但 XMLHttpRequest 似乎是通过 HTTP 发出的。
我什至尝试将 URL 更改为绝对 URL ( https://larsendt.com/jsontest/randomdata ),但它仍然将请求发送到我网站的 HTTP 版本。
当然,由于请求将发送到不同的协议(protocol),因此 ajax 调用会失败(跨域等)。
据 Chrome 报道:
The page at https://larsendt.com/jsontest/ displayed insecure content from http://larsendt.com/jsontest/randomdata/?ymax=500&count=32&t=0.08111811126582325.
我能想到的唯一其他相关信息是我让 nginx 从 http://larsendt.com 进行 301 重定向。至 https://larsendt.com ,但我不知道这会如何破坏任何东西(我相信这是相当标准的做法)。
如果您想要现场演示,损坏的版本仍然在 https://larsendt.com/jsontest .
无论如何,提前致谢。
最佳答案
尝试修复 URL,以便您的服务器不必重定向
url: "/jsontest/randomdata/" // there was a missing trailing /
// i.e. https://larsendt.com/jsontest/randomdata?ymax=500&count=32&t=0.9604179110508643
// was going to https://larsendt.com/jsontest/randomdata/?ymax=500&count=32&t=0.9604179110508643
关于jQuery ajax 不会发出 HTTPS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13556772/