【问题标题】:jQuery ajax won't make HTTPS requestsjQuery ajax 不会发出 HTTPS 请求
【发布时间】:2012-11-13 10:52:25
【问题描述】:

我正在我的网站上做一些非常基本的 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 版本。

自然,由于请求将发送到不同的协议,因此 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.comhttps://larsendt.com 的 301 重定向,但我不认为这会破坏任何东西(我相信这是相当标准的做法)。

如果您想要现场演示,损坏的版本仍在https://larsendt.com/jsontest

无论如何,提前致谢。

【问题讨论】:

  • 您介意更改帖子的标题,以使有此查询的人不会登陆此页面..

标签: jquery ajax https nginx


【解决方案1】:

301 永久重定向可能正在发生。要检查运行 Fiddler 并观察 Result 列。通常是 200 个代码,但我发现了一个 301 代码。

https jquery ajax 调用重定向到 http,导致混合内容错误。

【讨论】:

    【解决方案2】:

    尝试修复 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
    

    【讨论】:

    • 啊!做到了。非常感谢!
    • 好点,Nood。这可能也可以在服务器中修复。 IIRC,在 Apache(也许还有其他)中有几种不同的方法来处理丢失的斜杠。我不是专家,但确实读过一次(大约 10 年前!)。
    • 嗨!我遇到了同样的问题,但是我尝试进行 tomcat 身份验证,所以我不得不将 POST 重定向到正在重定向的j_security_check...我在 url 中没有什么要修复的...还有其他想法吗?
    • @PatrickFerreira “我在 url 中没有什么要修复的。”那么就不是同一个问题了。发布问题。
    • 我简直不敢相信。非常感谢!
    猜你喜欢
    • 2017-01-14
    • 1970-01-01
    • 2016-11-25
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    • 2014-12-01
    • 2016-04-29
    • 2023-03-26
    相关资源
    最近更新 更多