【问题标题】:Why is .ajaxForm() not working when the site is loaded over https?为什么通过 https 加载网站时 .ajaxForm() 不起作用?
【发布时间】:2023-03-15 06:49:01
【问题描述】:

我已将页面缩减到最低限度以重现此问题。它看起来像这样:

<!DOCTYPE html>
<html lang="en">
<head>
    {% load staticfiles %}
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script> 
    <script src="//malsup.github.com/jquery.form.js"></script> 
    <script type="text/javascript">
        $(document).ready(
                function() {
                    $('#signupform').ajaxForm(
                            function(new_html) {
                                alert("testing!");
                            });
                });
    </script>
</head>

<body>
    <form id="signupform" action="{% url 'index' %}" method="post">
            {% csrf_token %}
            {{ form.email }}
            {{ form.university }}
            <button type="submit">
                GET EARLY ACCESS
            </button>
    </form>
</body>
</html>

基本上只是一个 from 和一些使用 .ajaxForm() 来修改表单的 jquery。

当我通过 HTTP 加载网站时,它按预期工作:当按下按钮时,会出现“正在测试!”的警报。显示,POST是在后台发出的,没有重定向。但是,当我通过 HTTPS 运行它时,会出现问题,就像 javascript 甚至不存在一样。当我单击提交按钮时,没有警报并且站点正常重定向。

我已经在本地使用 HTTP 和标准 $ python manage.py runserver 进行了测试,并使用 HTTPS 使用 django-sslserver$ python manage.py runsslserver 进行了测试。我还在我们的 Heroku 登台服务器上运行了该应用程序,并获得了完全相同的 http/https 差异。

当网站通过 https 而不是 http 加载时,可能导致此 javascript 行为不同的原因是什么?

更新:根据评论,这里是 javascript 控制台输出:

混合内容:“https://localhost:8000/”处的页面已加载完毕 HTTPS,但请求了不安全的脚本 'http://malsup.github.io/jquery.form.js'。该请求已 受阻;内容必须通过 HTTPS 提供。

这会改变问题的性质。现在的问题是:为什么是

//malsup.github.com/jquery.form.js

没有通过 https 加载? (不确定我是否应该删除并重新发布这个新问题)

【问题讨论】:

标签: jquery ajax django


【解决方案1】:

https://malsup.github.com/jquery.form.js 执行 301 重定向到 http://malsup.github.com/jquery.form.js 最终成为不安全的资源。因此错误。坦率地说,我不知道他们为什么要这样做。那就是阻止你的问题。

【讨论】:

  • 有趣。我可以尝试下载 jquery.form.js 并将其存储为静态文件。这会是一个可行的解决方案吗?
  • 是的,您可以这样做或查看其他提供 jquery.form.js 的 CDN
  • 如果它有效,您可以投票或接受它,尽管它与原始问题略有不同。
  • 这个 cdn 为我工作
猜你喜欢
  • 2021-12-23
  • 2014-07-19
  • 1970-01-01
  • 1970-01-01
  • 2016-04-03
  • 2013-01-28
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
相关资源
最近更新 更多