Javascript jQuery在执行Python脚本PHP时加载img

Javascript jQuery在执行Python脚本PHP时加载img,javascript,php,jquery,python,Javascript,Php,Jquery,Python,我在jQuery和Python上摸索了一下,我试图显示一个非常简单的页面,显示一个正在加载的img,而一个长度稍长的Python脚本则获取我需要显示的信息 当然,当Python脚本准备好时,我希望隐藏加载图像并在它的位置显示Python脚本的结果 到目前为止,我已经能够通过一些谷歌帮助为我的网页拼凑出这一点: <html> <head> <img id="img" src="loader.gif"> <script src="assets

我在jQuery和Python上摸索了一下,我试图显示一个非常简单的页面,显示一个正在加载的img,而一个长度稍长的Python脚本则获取我需要显示的信息

当然,当Python脚本准备好时,我希望隐藏加载图像并在它的位置显示Python脚本的结果

到目前为止,我已经能够通过一些谷歌帮助为我的网页拼凑出这一点:

<html>
<head>
    <img id="img" src="loader.gif">
    <script src="assets/js/jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function() {
       url: "http://localhost/test.py";
       $.post(url, function(data){
          $("#id").html(data);
      });
    $("#img").hide();
    });
    </script>
</head>
</html>

$(文档).ready(函数(){
url:“http://localhost/test.py";
$.post(url、函数(数据){
$(“#id”).html(数据);
});
$(“#img”).hide();
});
任何帮助都将不胜感激。谢谢

这部分

$("#id").html(data);
这意味着jQuery正在用Python脚本的响应将id“id”填充元素。问题是您没有一个元素具有
id=“id”

您还需要做的是放入
$(“#img”).hide()
$的成功处理程序中。post
。这样,图像在
$.post
完成时隐藏。对于当前代码,它会立即隐藏,因为
$.post
是一个异步请求,因此任何其他代码都不会等待它

您的代码应该如下所示:

<html>
<head>
    <img id="img" src="loader.gif">
    <div id="id"></div>
    <script src="assets/js/jquery-1.9.1.min.js"></script>
    <script>
    $(document).ready(function() {
       url: "http://localhost/test.py";
       $.post(url, function(data){
          $("#id").html(data);
          $("#img").hide();
       });
    });
   </script>
</head>
</html>

这对我来说很有效:

Index.php

<!DOCTYPE html>
<html>
<head>
<title>Python Loading IMG Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" >
<script src="src/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<body>

<div id="container">
    <div id="loading">
        <img src="src/loading.gif" alt="loading_icon" />
    </div>
    <div id="results"></div>
</div>

<script language="javascript" type="text/javascript">
var URL = 'cgi-bin/test.py'; 

function read(){
$('#loading').show();
$.get(
    URL,
    {},
    function(result){
        $('#loading').hide();
        $('#results').html(result);     
    },
    'text'
).fail(function() {
    alert('Wrong Python URL');
});
}
read();

</script>
</body>
</html>

没有id为“id”的元素,谢谢Simon。然而,它似乎并没有隐藏加载的图像,也没有输出python文本。有没有可能因为python文本没有得到输出,它就被卡住了?为什么它也没有隐藏img?
<!DOCTYPE html>
<html>
<head>
<title>Python Loading IMG Page</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8" >
<script src="src/jquery-1.10.2.min.js" type="text/javascript"></script>
</head>
<body>

<div id="container">
    <div id="loading">
        <img src="src/loading.gif" alt="loading_icon" />
    </div>
    <div id="results"></div>
</div>

<script language="javascript" type="text/javascript">
var URL = 'cgi-bin/test.py'; 

function read(){
$('#loading').show();
$.get(
    URL,
    {},
    function(result){
        $('#loading').hide();
        $('#results').html(result);     
    },
    'text'
).fail(function() {
    alert('Wrong Python URL');
});
}
read();

</script>
</body>
</html>
#!/usr/bin/env python

print "Content-type: text/html\r\n"
print "\r\n" 
print "This is where the text goes."
print "Make sure you use HTML tags."