jQuery ajax 不会发出 HTTPS 请求

标签 jquery ajax https nginx

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

相关文章:

javascript - 无法使用 Angular ng-repeat 和 jquery 在 HTML 表中显示行

mysql - 为什么 ValueList 函数在对 MySQL 查询使用 concat 后返回意外的字符串?

C# 在客户端通过 HTTPS 维护 session

java - 发送 POST 到 HTTPS - 逻辑问题

javascript - PF 未在 window.onLoad 中定义

javascript - 在 change 事件上更改 <td> 元素值

c# - Mvc 4 动态表单文件上传

javascript - 从 php 服务器处理数据库中的 ajax 流式传输进度

javascript - 如果实例化类,PHP 不会向 ajax 返回任何内容

ssl - Comodo 通配符 ssl 证书和 Haproxy