jQuery ajax 不会发出 HTTPS 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13556772/
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
jQuery ajax won't make HTTPS requests
提问by Dane Larsen
I'm doing some pretty basic jQuery ajax stuff on my website, and I'm having a boatload of trouble.
我正在我的网站上做一些非常基本的 jQuery ajax 东西,但我遇到了很多麻烦。
Here's the relevant code:
这是相关的代码:
$(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);
}
});
});
});
The page is loaded over HTTPS, but the XMLHttpRequests appear to go out over HTTP.
该页面是通过 HTTPS 加载的,但 XMLHttpRequests 似乎是通过 HTTP 发出的。
I've attempted even changing the url to the absolute url (https://larsendt.com/jsontest/randomdata), and it stillsends the request to the HTTP version of my site.
我什至尝试将 url 更改为绝对 url ( https://larsendt.com/jsontest/randomdata),但它 仍然将请求发送到我网站的 HTTP 版本。
Naturally, since the request is going to a different protocol, the ajax call fails (cross-domain and all that).
自然,由于请求将发送到不同的协议,因此 ajax 调用失败(跨域等等)。
As reported by Chrome:
据 Chrome 报道:
The page at https://larsendt.com/jsontest/ displayed insecure content from http://larsendt.com/jsontest/randomdata/?ymax=500&count=32&t=0.08111811126582325.
The only other relevant information I can think of is that I'm having nginx do a 301 redirect from http://larsendt.comto https://larsendt.com, but I don't see how that would break anything (I believe it's fairly standard practice).
我能想到的唯一其他相关信息是我让 nginx 从http://larsendt.com进行301 重定向到https://larsendt.com,但我不知道这会如何破坏任何东西(我相信这是相当标准的做法)。
If you want a live demo, the broken version is still up at https://larsendt.com/jsontest.
如果您想要现场演示,损坏的版本仍在https://larsendt.com/jsontest 上。
Anyway, thanks in advance.
无论如何,提前致谢。
回答by Popnoodles
Try fixing the URL so your server doesn't have to redirect
尝试修复 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
回答by SushiGuy
301 Permanent Redirect may be happening. To check run Fiddler and watch the Result column. Usually 200 codes, but I spotted a 301 code.
301 永久重定向可能正在发生。要检查运行 Fiddler 并观察结果列。通常是 200 个代码,但我发现了一个 301 代码。
The httpsjquery ajax call was redirecting to http, causing the Mixed Content error.
该HTTPSjQuery的AJAX调用被重定向到HTTP,造成混合内容错误。