【问题标题】:Raspberry Pi - Flask Server using JQuery not working Offline (Online it works)Raspberry Pi - 使用 JQuery 的 Flask 服务器无法离线工作(在线工作)
【发布时间】:2016-12-12 14:48:56
【问题描述】:

我是一个初学者,我有一个应该很容易解决的问题。但是,在研究了许多论坛并尝试了技术解决方案之后,我无法使代码离线工作,但它确实可以很好地在线加载 JQuery。让我开始解释我到目前为止的工作。

我使用 Flask 创建了一个应用程序,因此我可以使用智能手机应用程序或导航器(Raspberry 本地主机)操作检查机器人。我写了 2 个代码,Python 服务器和 HTML 客户端。它在网上工作得很好,得到如下的 JQuery 库:

<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>

现在我想让它离线工作,我尝试了本网站其他问题中给出的许多解决方案,但到目前为止我无法让它工作。 我尝试下载 JQuery 并将其放在同一个文件夹中并在代码中引用它,但没有成功。这是我想的。

<script src="jquery-1.9.1.min.js" type="text/javascript"></script>

我确保放置了相同的名称和文件夹,尝试了子文件夹,有无“/”,但没有任何效果。

目前我也在使用在线 css(用于图像),但我在测试离线 JQuery 时也尝试将其删除。我只是想让 JQuery 离线工作,这样我就可以完成页面。

再次,我是新手,所以我很抱歉并提前感谢您的帮助。

这是完整的 HTML 文件,可以在网上正常工作,它有一些葡萄牙语,但它们并不重要,因为问题只是在本地加载 Jquery。再次感谢您的关注。

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<!doctype html>
<html>
<head>
<meta name="viewport" content="initial-scale=1, maximum-scale=1">
<link rel="stylesheet" href="http://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css">
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head> 

<style>
h3 {text-align: left;}
span {font-weight: bold;}
</style>

<script type=text/javascript>
    $(
    // Ao pressionamento de um botão, realizar um pedido ajax para o servidor realizando a determinada função
    function() 
    {
        $('#frente').click(function() 
        {
        $.getJSON('/_frente');
        }); 
	$('#re').click(function() 
        {
        $.getJSON('/_re');
        });
	$('#esquerda').click(function() 
        {
        $.getJSON('/_esquerda');
        });
	$('#direita').click(function() 
        {
        $.getJSON('/_direita');
        });
	$('#verticalcima').click(function() 
        {
        $.getJSON('/_verticalcima');
        });
	$('#verticalbaixo').click(function() 
        {
        $.getJSON('/_verticalbaixo');
        }); 
	$('#posinit').click(function() 
        {
        $.getJSON('/_posinit');
        }); 
	$('#azimuteesquerda').click(function() 
        {
        $.getJSON('/_azimuteesquerda');
        }); 
	$('#azimutedireita').click(function() 
        {
        $.getJSON('/_azimutedireita');
        }); 
        $('#d').click(function() 
        {
        $.getJSON('/_distancia', function(data)
            {
                $("#distaprox").text(data.distaprox);
            });
        });
        
    }
    );

</script>
<!-- Uma simples pagina JQuery Mobile para realizar o controle remoto do sistema robotico de inspeção -->

<body>
  <div data-role="header">
    <div><h3>ELMO CRSEA IFF</h3></div>
  </div>
  <div style="width:400px;;">
    <form>
    <div id="float1" style="width:200px;float:left;">
    <p>&nbsp;Direcional:</p>
    <p>&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" name="frente" id="frente" style="font-size:40px"><i class="fa fa-arrow-up"></i></button>
    </p>
    <p>&nbsp;<button type="button" name="esquerda" id="esquerda" style="font-size:40px"><i class="fa fa-arrow-left"></i></button>
    &nbsp&nbsp&nbsp&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" name="direita" id="direita" style="font-size:40px"><i class="fa fa-arrow-right"></i></button>
    </p>	
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" name="re" id="re" style="font-size:40px"><i class="fa fa-arrow-down"></i></button>
    </p>
    </div>
    <div id="float2" style="200px;float:left;">
    <p>&nbsp;Câmera:</p>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" name="verticalcima" id="verticalcima" style="font-size:40px"><i class="fa fa-toggle-up"></i></button>
    </p>
    <button type="button" name="azimuteesquerda" id="azimuteesquerda" style="font-size:40px"><i class="fa fa-toggle-left"></i></button>
    &nbsp;&nbsp;<button type="button" id="posinit" name="posinit" style="height:50px;width:50px;font-size:15px">Inicial<i</i></button>
    &nbsp;<button type="button" name="azimutedireita" id="azimutedireita" style="font-size:40px"><i class="fa fa-toggle-right"></i></button>
    </p>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp&nbsp;
    <button type="button" name="verticalbaixo" id="verticalbaixo" style="font-size:40px"><i class="fa fa-toggle-down"></i></button></p>
    </div>
    <p>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <button type="button" id="d" name="d" style="height:50px;width:50px;font-size:15px">Medir<i</i></button> 
    Distância aproximada = <span id="distaprox"></span>cm</p>
    </form>
  </div>

  <footer>
    <p><h3>Copyright 2016 IBL</h3></p>
  </footer>
</body>
</html>

【问题讨论】:

    标签: jquery python flask raspberry-pi jinja2


    【解决方案1】:

    尝试创建一个静态文件夹来存放您的所有 js 和其他静态文件,如下所示:

    static/
         - js/
         - css/
         - ...
    

    现在,将您的 jquery 文件放入 js/ 文件夹并在您的模板中使用以下内容:

    url_for('static', filename='js/jquery-1.9.1.min.js')
    

    在您的模板中,您将拥有:

    <script src="{{ url_for('static', filename='js/jquery-1.9.1.min.js') }}" type="text/javascript"></script>
    

    【讨论】:

    • 非常感谢!我花了几个小时试图找到解决方案。感谢您花时间帮助我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多